【发布时间】:2017-05-28 07:28:48
【问题描述】:
我尝试使用独立函数获取为数组分配的字节数,但失败了。我的代码如下:
#include <stdio.h>
void testing(char *source);
int main(){
char a[12] = "Hello World!";
//printf("%d",sizeof(a)); Prints 12. I want to get this value
testing(a); // Prints 4. I need to get 12 by using functions
return 0;
}
void testing(char *source){
printf("%d",sizeof(source));
}
我想得到 12 的结果,但输出告诉我它是 4。
【问题讨论】:
-
无法通过指针从
sizeof获取数组的大小。 -
这就像说“我希望 2+2 为 5”。你可以继续“想要”它,但它不会发生。
sizeof不会这样做。 -
将函数改为
void testing( char (*source)[12] ),然后在函数中使用sizeof *source