问题
#include <stdio.h>
#define R 10
#define C 20
int main()
{
int *p;
int *p1[R];
int *p2[R][C];
printf("%d %d %d", sizeof(*p),sizeof(*p1),sizeof(*p2));
getchar();
return 0;
}
Why is the output: 4 8 160
? Why does size of p1
becomes 8 and not 4?
回答1:
Consider the types.
sizeof(*p)
==>sizeof(int)
sizeof(*p1)
==>sizeof(int *)
sizeof(*p2)
==>sizeof((int [20]))
Note: Depending on your platform and compiler, you'll get different results.
Also, as we know, sizeof
produces a result of type size_t
, it's advised to use %zu
format specifier to print the result.
来源:https://stackoverflow.com/questions/31556285/why-size-is-different-for-different-pointers