2

I am getting different values on sizeof for the same string value.

when I run the below code in C:

char str[] = "November";
char *s = "November";
printf(" str[] = %ld\n",sizeof(str));
printf(" *s = %ld\n",sizeof(s));

expected output:

str[] = 9
*s = 9

Actual output:

str[] = 9
*s = 8

What is going on actually?

Sijo John
  • 45
  • 4

2 Answers2

4
  • sizeof(str) is the size of char array str which contains 9 elements (including the null terminator) and therefore 9 bytes.

  • sizeof(s) is the size of pointer to char which is 8 bytes on your system

P.W
  • 26,289
  • 6
  • 39
  • 76
3

sizeof() is not strlen()

the size of a pointer is not the size of the pointed element

bruno
  • 32,421
  • 7
  • 25
  • 37