C – SizeOF pointer

char c[] = {'a','b','c'};
int* p = &c[0];
printf("%i ", sizeof(*p)); //Prints out 4
printf("%i ", sizeof(*c)); //Prints out 1

I am very confused about this part of the code. Both p and c represent the address of the array c at the 0th index. But why does sizeof(* p) print 4? Shouldn’t it be 1?

Because the type of p is int *, the type of * p is int, in your implementation The above is obviously 4 bytes wide.

If you don’t want the program to call undefined behavior, please use %zu to print size_t (yield size).

< /p>

char c[] = {'a','b','c'};
int* p = &c[0];
printf("%i ", sizeof(*p)); //Prints out 4
printf("%i ", sizeof(*c)); //Prints out 1

I am This part of the code is very confusing. Both p and c represent the address of the array c at the 0th index. But why does sizeof(*p) print 4? Shouldn’t it be 1?

Because the type of p is int*, the type of *p is int, which is obviously 4 bytes wide in your implementation.

If you don’t want the program to call undefined behavior, please use %zu to print size_t (output size).

Leave a Comment

Your email address will not be published.