puzzled by sizeof(string)
void setup()
{
serial.begin(9600);
}
void loop()
{
char cmcs[15]="8613800571500";
get(cmcs);
delay(2000);
}
void get(char cmcsd[]){
serial.println(sizeof(cmcsd));
}
why result 2,but not 15
{
serial.begin(9600);
}
void loop()
{
char cmcs[15]="8613800571500";
get(cmcs);
delay(2000);
}
void get(char cmcsd[]){
serial.println(sizeof(cmcsd));
}
why result 2,but not 15
jerry,
if trying find out how many characters there in string (from start nul terminator), should use strlen
sizeof gives number of bytes taken thing inside parentheses. you passed character pointer, uses 2 bytes of memory.
if have array of fixed size in routine, sizeof can tell how many bytes whole array takes up, when use sizeof within routine. this 1 of subtle differences between array , pointer. for example
-mike
if trying find out how many characters there in string (from start nul terminator), should use strlen
code: [select]
void get(char cmcsd[]){
serial.println(strlen(cmcsd));
}
sizeof not function. it's calculated compiler when program compiled, , of course compiler can't know how many characters going put string.sizeof gives number of bytes taken thing inside parentheses. you passed character pointer, uses 2 bytes of memory.
if have array of fixed size in routine, sizeof can tell how many bytes whole array takes up, when use sizeof within routine. this 1 of subtle differences between array , pointer. for example
code: [select]
char my_string[25];
serial.println(sizeof(my_string));will print out 25.-mike
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > puzzled by sizeof(string)
arduino
Comments
Post a Comment