void sizeof_test2();
void sizeof_test2()
{
int array[5];
size_t arr_size = sizeof(array);
printf( "sizeof:\n"
"array = %d\n"
"arr_size = %d\n", sizeof(array), sizeof(arr_size));
}
GCC 编译器输出:
sizeof_test2.c: In function `sizeof_test2':
sizeof_test2.c:6: error: `size_t' undeclared (first use in this function)
sizeof_test2.c:6: error: (Each undeclared identifier is<br>
reported only once sizeof_test2.c:6: error: for each function it<br>
appears in.) sizeof_test2.c:6: error: parse error before "arr_size"<br>
sizeof_test2.c:10: error: `arr_size' undeclared (first use in this<br>
function) make[2]: [build/Debug/Cygwin-Windows/sizeof_test2.o]<br>
Error 1 make[1]: [.build-conf] Error 2<br>
不知道为什么会收到此错误,通过 printf
显示 size_t 类型的正确方法是什么?
最佳答案
size_t
类型在 stddef.h
header (以及其他 header ,例如 stdio.h
)中定义。
请注意,在您的程序中,您正在使用 printf
函数,因此您必须包含 stdio.h
。
关于c - 理解 size_t 类型和 sizeof 运算符的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12587406/