+ 3

Why the output is 50?

#include <stdio.h> #define A 5 #define B 10 /*suppose size of pointer is 4 bytes*/ int main() { char (*p)[A][B]; printf("%u",sizeof(*p)); return 0; } Here comes code but i wonder why won't we multiply A x B x 4 == 5 x 10 x 4 which is 200 but I don't why answer is 50 , please someone explain this..

8th Jun 2020, 5:50 AM
Nikhil Maroju
Nikhil Maroju - avatar
2 Respuestas
+ 2
C types can look ugly and not very obvious some times. p is a pointer to char[A][B]. Therefor: sizeof(p) == sizeof a pointer == 4 sizeof(*p) == A * B * sizeof(char) == 50 What you expected is, if you define p like char *p[A][B]; without parentheses. Now p is a char*[A][B].
8th Jun 2020, 6:22 AM
Schindlabua
Schindlabua - avatar
+ 2
Because you aren't getting the size of the pointer when you use the sizeof function, you are getting the size of the char array which is 5x10x1=50 bytes. If you want to get the size of the pointer, you should use sizeof(p), which will be equal to 8 bytes, not 200. Because pointers' size doesn't change according to what they're pointing to.
8th Jun 2020, 6:27 AM
ogoxu
ogoxu - avatar