Why Size is different for different pointers

落爺英雄遲暮 提交于 2019-12-13 09:23:57

问题


#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.

  1. sizeof(*p) ==> sizeof(int)
  2. sizeof(*p1) ==> sizeof(int *)
  3. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!