为什么大小对于不同的指针是不同的 [英] Why Size is different for different pointers

查看:88
本文介绍了为什么大小对于不同的指针是不同的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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;
}

为什么输出:4 8 160?为什么p1的大小变成8而不是4?

Why is the output: 4 8 160? Why does size of p1 becomes 8 and not 4?

推荐答案

考虑类型.

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

另外,我们知道,sizeof会生成类型为size_t的结果,建议使用%zu格式说明符来打印结果.

Also, as we know, sizeof produces a result of type size_t, it's advised to use %zu format specifier to print the result.

这篇关于为什么大小对于不同的指针是不同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆