关于“大小”的问题ch的炭指针阵列
我有两个char阵列如下:
char *t1[10];
char(*t2)[10];
当使用sizeof查找它们的尺寸时,
printf("sizeof(t1): %d\n", sizeof(t1));
printf("sizeof(t2): %d\n", sizeof(t2));
我发现输出是:
sizeof(t1): 80
sizeof(t2): 8
我对为什么使用SizeOf Operator在使用Sizeof Operator时会有两个不同的结果感到困惑。
I have two char arrays as below:
char *t1[10];
char(*t2)[10];
when using sizeof to find their size
printf("sizeof(t1): %d\n", sizeof(t1));
printf("sizeof(t2): %d\n", sizeof(t2));
I found that the output is:
sizeof(t1): 80
sizeof(t2): 8
I am quite confused by why I have two different results when using the sizeof operator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于初学者,您必须使用转换说明器
zu
而不是d
在输出类型size> size_t
的值时,此记录
不会声明一个数组。它是指向数组类型
char [10]
的指针的声明。因此,
sizeof(t1)
产生数组的大小,而sizeof(t2)
产生指针的大小。考虑例如声明,
指针
t2
是由数组t1
的第一个元素(类型char [10]
)的第一个元素的地址初始化的。 。这就是指向数组t1
的第一个元素。另外,请考虑以下
printf
呼叫的输出为
10
的呼叫。这是指指指针您将获得类型
char [10]
的一维数组。如果您想获得相同的输出,则应像
For starters you have to use the conversion specifier
zu
instead ofd
when outputting values of the typesize_t
This record
does not declare an array. It is a declaration of a pointer to the array type
char[10]
.So
sizeof( t1 )
yields the size of an array whilesizeof( t2 )
yields the size of a pointer.Consider for example declarations
The pointer
t2
is initialized by the address of the first element (of the typechar[10]
) of the arrayt1
. That is it points to the first element of the arrayt1
.Also consider the following call of
printf
The output of the call will be
10
.That is dereferencing the pointer you will get one dimensional array of the type
char[10]
.If you want to get identical outputs then the second declaration should be rewritten like