为什么数组参数的大小与main中的大小不一样? [英] Why isn't the size of an array parameter the same as within main?

查看:117
本文介绍了为什么数组参数的大小与main中的大小不一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么数组的大小不会像参数一样在main中相同?

  #include< stdio.h中> 

void PrintSize(int p_someArray [10]);

int main(){
int myArray [10];
printf(%d\\\
,sizeof(myArray)); / *正如所料,40 * /
PrintSize(myArray); / *打印4,而不是40 * /
}

PrintSize(int p_someArray [10]){
printf(%d \ n,sizeof(p_someArray));
}


解决方案



所以,

,数组类型隐式地转换为指针类型时传递给函数。 b
$ b

  void PrintSize(int p_someArray [10]){
printf(%zu\\\
,sizeof(p_someArray));
}

  void PrintSize(int * p_someArray){
printf(%zu\\\
,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*)

这篇关于为什么数组参数的大小与main中的大小不一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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