关于“大小”的问题ch的炭指针阵列

发布于 2025-02-04 03:59:08 字数 412 浏览 2 评论 0原文

我有两个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒浓于脸红 2025-02-11 03:59:08

对于初学者,您必须使用转换说明器zu而不是d在输出类型size> size_t的值时,

printf("sizeof(t1): %zu\n", sizeof(t1));
printf("sizeof(t2): %zu\n", sizeof(t2));

此记录

char(*t2)[10];

不会声明一个数组。它是指向数组类型char [10]的指针的声明。

因此,sizeof(t1)产生数组的大小,而sizeof(t2)产生指针的大小。

考虑例如声明,

char t1[5][10];
char ( *t2 )[10] = t1;

指针t2是由数组t1的第一个元素(类型char [10])的第一个元素的地址初始化的。 。这就是指向数组t1的第一个元素。

另外,请考虑以下printf呼叫的

printf("sizeof( *t2 ): %zu\n", sizeof( *t2 ));

输出为10的呼叫。

这是指指指针您将获得类型char [10]的一维数组。

如果您想获得相同的输出,则应像

char *t1[10];
char *( t2)[10];

For starters you have to use the conversion specifier zu instead of d when outputting values of the type size_t

printf("sizeof(t1): %zu\n", sizeof(t1));
printf("sizeof(t2): %zu\n", sizeof(t2));

This record

char(*t2)[10];

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 while sizeof( t2 ) yields the size of a pointer.

Consider for example declarations

char t1[5][10];
char ( *t2 )[10] = t1;

The pointer t2 is initialized by the address of the first element (of the type char[10]) of the array t1. That is it points to the first element of the array t1.

Also consider the following call of printf

printf("sizeof( *t2 ): %zu\n", sizeof( *t2 ));

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

char *t1[10];
char *( t2)[10];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文