找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 265|回复: 1

Question6.21:Why doesn't sizeof properly report the size of an array。。。

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-1 13:26:34 | 显示全部楼层 |阅读模式

Q: Why doesn't sizeof properly report the size of an array when the array is a parameter to a function? I have a test routine
                  f(char a[10])
                  {
                             int i = sizeof(a);
                             printf("%d\n", i);
                  }and it prints 4, not 10.

A: The compiler pretends that the array parameter was declared as a pointer (that is, in the example, as char *a; see question 6.4), and sizeof reports the size of the pointer. See also questions 1.24 and 7.28.

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-1 13:34:17 | 显示全部楼层
问题:当一个数组作参数传递时,为什么sizeof不能正确的报告该数组的空间大小?我有一个测试程序:
       f(char a[10])
             {
                    int i=sizeof(a);
                    printf("%d\n",i);
              }
并且它打印出4,而不是10.
回答:当数组作为参数传递的时候,编译器假设它是作为一个指针的(在这个例子中,它就是char * a了,参考问题6.4),sizeof得到的是指针的大小。参考问题1.24和7.28。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-1 16:39 , Processed in 0.077021 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表