SIZEOF在C编程语言中数组? [英] Sizeof an array in the C programming language?
本文介绍了SIZEOF在C编程语言中数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么不是作为参数发送相同的内主?
数组的大小 的#include<&stdio.h中GT;空隙PrintSize(中间体p_someArray [10]);诠释主(){
INT myArray的[10];
的printf(%d个\\ N的sizeof(myarray的)); / *正如所料,40 * /
PrintSize(myarray的); / *打印4,而不是40 * /
}空隙PrintSize(中间体p_someArray [10]){
的printf(%d个\\ N的sizeof(p_someArray));
}
解决方案
否后,数组类型为隐转换成当你把它传递到一个函数指针类型
因此,
无效PrintSize(INT p_someArray [10]){
的printf(%祖\\ N的sizeof(p_someArray));
}
和
无效PrintSize为(int * p_someArray){
的printf(%祖\\ N的sizeof(p_someArray));
}
是等效的。所以,你得到的是价值的 的sizeof(INT *)
Why isn't the size of an array sent as a parameter the same as within main?
#include <stdio.h>
void PrintSize(int p_someArray[10]);
int main () {
int myArray[10];
printf("%d\n", sizeof(myArray)); /* As expected, 40 */
PrintSize(myArray);/* Prints 4, not 40 */
}
void PrintSize(int p_someArray[10]){
printf("%d\n", sizeof(p_someArray));
}
解决方案
No, array-type is implicitly converted into pointer type when you pass it in to a function.
So,
void PrintSize(int p_someArray[10]) {
printf("%zu\n", sizeof(p_someArray));
}
and
void PrintSize(int *p_someArray) {
printf("%zu\n", sizeof(p_someArray));
}
are equivalent. So what you get is the value of sizeof(int*)
这篇关于SIZEOF在C编程语言中数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文