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 the result is 2,but not 15

Jerry,

If you are trying to find out how many characters there are in a string (from the start up to the NUL terminator), you should use strlen

void get(char cmcsd[]){
Serial.println(strlen(cmcsd));
}

sizeof is not a function. It's calculated by the compiler when your program is compiled, and of course the compiler can't know how many characters you are going to put into your string.

sizeof gives the number of bytes taken up by the thing inside the parentheses. You passed it a character pointer, which uses two bytes of memory.

If you have an array of fixed size in a routine, sizeof can tell you how many bytes the whole array takes up, when you use sizeof within that routine. This is one of the subtle differences between an array and a pointer. For example

char my_string[25];
Serial.println(sizeof(my_string));

will print out 25.

-Mike

Get it.Thanks for Mike's reply.