
1) Why is sizeof operator and not function
2) On a side note, something like this is illegal...
printf("%u\n", sizeof(main));
The sizeof operator shall not be applied to an expression that has function type.
Egs.
1)
main()
{
char *p="GOOD";
char a[ ]="GOOD";
printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p),
sizeof(*p), strlen(p));
printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a));
}
Answer:
sizeof(p) = 2, sizeof(*p) = 1, strlen(p) = 4
sizeof(a) = 5, strlen(a) = 4
Explanation:
sizeof(p) => sizeof(char*) => 2
sizeof(*p) => sizeof(char) => 1
Similarly,
sizeof(a) => size of the character array => 5
When sizeof operator is applied to an array it returns the sizeof the array
and it is not the same as the sizeof the pointer variable. Here the sizeof(a)
where a is the character array and the size of the array is 5 because the
space necessary for the terminating NULL character should also be taken
into account.
0 comments:
Post a Comment