我试图获取使用独立函数为阵列分配的字节数,但是我失败了。我的代码如下:
#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。
答案 0 :(得分:0)
我想将结果设为12,但输出告诉我它是4。
当数组作为函数指针传递时,你无法获得数组的大小。
在void testing(char *source)
中,source
是指针,因此sizeof( pointer )
为您提供4(机器中为4个字节)。
无法找到函数内部的数组大小,因为数组参数无论如何都会衰减到指针。