(C) String array: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
(3 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
Zeile 10: | Zeile 10: | ||
const char directions2[3][6]={"up","in","off"}; | const char directions2[3][6]={"up","in","off"}; | ||
const char directions3[3][6]={"halt","stop","stopp"}; | const char directions3[3][6]={"halt","stop","stopp"}; | ||
− | |||
− | |||
− | for(uint8_t i= | + | for(uint8_t i=1;sizeof(directions1)/sizeof(directions1[0]);i++){ |
− | if (strcmp(payloadStr.c_str(), | + | if (strcmp(payloadStr.c_str(),directions1[i-1])==0)direction=1; |
} | } | ||
− | for(uint8_t i= | + | for(uint8_t i=1;sizeof(directions2)/sizeof(directions2[0]);i++){ |
− | if (strcmp(payloadStr.c_str(), | + | if (strcmp(payloadStr.c_str(),directions2[i-1])==0)direction=2; |
} | } | ||
− | for(uint8_t i= | + | for(uint8_t i=1;sizeof(directions3)/sizeof(directions3[0]);i++){ |
− | if (strcmp(payloadStr.c_str(), | + | if (strcmp(payloadStr.c_str(),directions3[i-1])==0)direction=3; |
} | } | ||
+ | |||
</pre> | </pre> | ||
+ | |||
+ | |||
+ | #include <stdio.h> | ||
+ | |||
+ | char b[3][4]; | ||
+ | |||
+ | int main(void){ | ||
+ | printf("Size of array b[3]=%d\n", sizeof(b[3])); | ||
+ | printf("Size of array b[2]=%d\n", sizeof(b[2])); | ||
+ | printf("Size of array b[5]=%d\n", sizeof(b[5])); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | b is a 2D character array of rows 3 and columns 4. | ||
+ | |||
+ | So, if you take sizeof(b) you will get 12. | ||
+ | |||
+ | b[0] (and b[i] in general) has a type of a 1D character array of size 4. So. if you take sizeof (b[0]) you will get 4. | ||
+ | |||
+ | b[0][0] (and b[i][j] in general) has a type of char. So if you take sizeof (b[0][0]) you will get 1. | ||
+ | |||
+ | sizeof does not depend on the array index. The type remains the same even for b[0] and b[100], even though it might be out of range of the memory of the array. | ||
=Quelle= | =Quelle= | ||
[https://overiq.com/c-programming-101/array-of-strings-in-c/ gute Quelle] | [https://overiq.com/c-programming-101/array-of-strings-in-c/ gute Quelle] |
Aktuelle Version vom 12. August 2022, 13:12 Uhr
Allgemeines
String in einem Array ist in C immer wieder ein Fallstrick, zumindest bei mir.
Ich benutze in meinen Prograemmchen schon mal ein String-Array um Strings zu vergleichen. Das sieht z.B. dann so aus.
uint8_t direction=0; // 1 means down, out; 2 means up, in const char directions1[3][6]={"down","out","on"}; const char directions2[3][6]={"up","in","off"}; const char directions3[3][6]={"halt","stop","stopp"}; for(uint8_t i=1;sizeof(directions1)/sizeof(directions1[0]);i++){ if (strcmp(payloadStr.c_str(),directions1[i-1])==0)direction=1; } for(uint8_t i=1;sizeof(directions2)/sizeof(directions2[0]);i++){ if (strcmp(payloadStr.c_str(),directions2[i-1])==0)direction=2; } for(uint8_t i=1;sizeof(directions3)/sizeof(directions3[0]);i++){ if (strcmp(payloadStr.c_str(),directions3[i-1])==0)direction=3; }
- include <stdio.h>
char b[3][4];
int main(void){
printf("Size of array b[3]=%d\n", sizeof(b[3])); printf("Size of array b[2]=%d\n", sizeof(b[2])); printf("Size of array b[5]=%d\n", sizeof(b[5]));
return 0;
}
b is a 2D character array of rows 3 and columns 4.
So, if you take sizeof(b) you will get 12.
b[0] (and b[i] in general) has a type of a 1D character array of size 4. So. if you take sizeof (b[0]) you will get 4.
b[0][0] (and b[i][j] in general) has a type of char. So if you take sizeof (b[0][0]) you will get 1.
sizeof does not depend on the array index. The type remains the same even for b[0] and b[100], even though it might be out of range of the memory of the array.