相关疑难解决方法(0)

查找函数内部的数组长度

在下面的程序中,数组的长度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 ×1

c ×1