从指针数组获取值-按引用调用

时间:2019-03-13 10:04:28

标签: c arrays function 2d

我想从函数的引用中获取值。 我的功能是:

void print2D_A(double (*ary2D)[3][4]){ ... }

主要:

double ary2D[3][4] = {
                        {10.1, 11.2, 12.3, 13.4},
                        {20.1, 21.2, 22.3, 23.4},
                        {30.1, 31.2, 32.3, 33.4}
                     };
print2D_A(&ary2D);

获取函数中的第一个值:

*(ary2D)+1

1 个答案:

答案 0 :(得分:0)

不要使用2D数组的地址,确实不清楚。

void print2D_A(double ary2D[3][4])    // `ary2D` is already a pointer of array, seems like `douible* ary2D`
{
    printf("%f\n", ary2D[0][0]);
}

编辑:

void print2D_B(double ary2D[3][4])    // `ary2D` is already a pointer of array, seems like `douible* ary2D`
{
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<4; j++)
        {
            printf("%f, ", ary2D[i][j]);
        }
        printf("\n");
    }
}
int main(int argc, char *argv[])
{
    double ary2D[3][4] = {
        { 10.1, 11.2, 12.3, 13.4 },
        { 20.1, 21.2, 22.3, 23.4 },
        { 30.1, 31.2, 32.3, 33.4 }
    };
    print2D_A(ary2D);    // In fact, this is a pointer of double array
    return 0;
}