如何通过c [duplicate]中的指针传递二维数组

时间:2022-09-10 16:53:26

Possible Duplicate:
Passing a pointer representing a 2D array to a function in C++

可能重复:将表示2D数组的指针传递给C ++中的函数

I am trying to pass my 2-dimensional array to a function through pointer and want to modify the values.

我试图通过指针将我的二维数组传递给函数,并希望修改值。

#include <stdio.h>

void func(int **ptr);

int main() {
    int array[2][2] = {
        {2, 5}, {3, 6}
    };

    func(array);

    printf("%d", array[0][0]);
    getch();
}

void func(int **ptr) {
    int i, j;
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            ptr[i][j] = 8;
        }
    }
}

But the program crashes with this. What did I do wrong?

但程序崩溃了。我做错了什么?

2 个解决方案

#1


3  

It crashes because an array isn't a pointer to pointer, it will try reading array values as if they're pointers, but an array contains just the data without any pointer.
An array is all adjacent in memory, just accept a single pointer and do a cast when calling the function:

它崩溃是因为数组不是指向指针的指针,它会尝试读取数组值,就好像它们是指针一样,但是数组只包含没有任何指针的数据。数组在内存中都是相邻的,只需接受一个指针并在调用函数时执行转换:

func((int*)array);

...

void func(int *ptr) {
    int i, j;
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            ptr[i+j*2]=8;
        }
    }
}

#2


9  

Your array is of type int[2][2] ("array of 2 array of 2 int") and its name will decay to a pointer to its first element which would be of type int(*)[2] ("pointer to array of 2 int"). So your func needs to take an argument of this type:

你的数组是int [2] [2]类型(“2个数组的2个数组的数组”),它的名称将衰减为指向其第一个元素的指针,该元素的类型为int(*)[2](“指针到数组2 int“)。所以你的func需要采用这种类型的参数:

void func(int (*ptr)[2]);
// equivalently:
// void func(int ptr[][2]);

Alternatively, you can take a reference to the array type ("reference to array of 2 array of 2 int"):

或者,您可以引用数组类型(“引用2个数组的2个数组的数组”):

void func(int (&ptr)[2][2]);

Make sure you change both the declaration and the definition.

确保更改声明和定义。

#1


3  

It crashes because an array isn't a pointer to pointer, it will try reading array values as if they're pointers, but an array contains just the data without any pointer.
An array is all adjacent in memory, just accept a single pointer and do a cast when calling the function:

它崩溃是因为数组不是指向指针的指针,它会尝试读取数组值,就好像它们是指针一样,但是数组只包含没有任何指针的数据。数组在内存中都是相邻的,只需接受一个指针并在调用函数时执行转换:

func((int*)array);

...

void func(int *ptr) {
    int i, j;
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            ptr[i+j*2]=8;
        }
    }
}

#2


9  

Your array is of type int[2][2] ("array of 2 array of 2 int") and its name will decay to a pointer to its first element which would be of type int(*)[2] ("pointer to array of 2 int"). So your func needs to take an argument of this type:

你的数组是int [2] [2]类型(“2个数组的2个数组的数组”),它的名称将衰减为指向其第一个元素的指针,该元素的类型为int(*)[2](“指针到数组2 int“)。所以你的func需要采用这种类型的参数:

void func(int (*ptr)[2]);
// equivalently:
// void func(int ptr[][2]);

Alternatively, you can take a reference to the array type ("reference to array of 2 array of 2 int"):

或者,您可以引用数组类型(“引用2个数组的2个数组的数组”):

void func(int (&ptr)[2][2]);

Make sure you change both the declaration and the definition.

确保更改声明和定义。