无法理解的sizeof返回值() [英] Cannot understand return value of sizeof()

查看:132
本文介绍了无法理解的sizeof返回值()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这在我的code:

I have this in my code:

int x = 4;
char* array = malloc(x*sizeof(char));
size_t arraysize = sizeof (array);
printf("arraysize: %zu\n", arraysize);

这code打印出来,

ARRAYSIZE:8

arraysize: 8

为什么是8,而不是4? (由于4 * sizeof的(焦)= 4 * 1)

Why is it 8 and not 4? (Since 4*sizeof(char) = 4 * 1)

推荐答案

阵列是在你的code的指针。因此,的sizeof(阵列)返回用C字节为指针(提示:C的字节可以在其中有超过8位)的大小。

array is a pointer in your code. sizeof(array) therefore returns the size of that pointer in C bytes (reminder: C's bytes can have more than 8 bits in them).

所以,8是鼠标指针的大小。

So, 8 is your pointer size.

此外,对于为size_t 正确的类型说明符的printf()的格式字符串为%祖

Also, the correct type specifier for size_t in printf()'s format strings is %zu.

这篇关于无法理解的sizeof返回值()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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