相关疑难解决方法(0)

为什么数组参数的大小与main中的大小不同?

为什么作为参数发送的数组的大小与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));
}
Run Code Online (Sandbox Code Playgroud)

c arrays function sizeof

101
推荐指数
7
解决办法
7万
查看次数

查找函数内部的数组长度

在下面的程序中,数组的长度ar在main中是正确的,但是temp它显示了ar我的计算机上指针的长度为2(以单位为单位sizeof(int)).

#include <stdio.h>

void temp(int ar[]) // this could also be declared as `int *ar`
{
    printf("%d\n", (int) sizeof(ar)/sizeof(int));
}

int main(void)
{
    int ar[]={1,2,3};
    printf("%d\n", (int) sizeof(ar)/sizeof(int));
    temp(ar);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想知道如何定义函数,以便在函数中正确读取数组的长度.

c arrays

18
推荐指数
4
解决办法
3万
查看次数

标签 统计

arrays ×2

c ×2

function ×1

sizeof ×1