将2D指针数组传递给具有1D指针数组参数的函数

时间:2021-03-05 21:33:13

I am currently doing a project where I am going to pass a 2D pointer array to a function. Here is the function implementation:

我目前正在做一个项目,我将把一个2D指针数组传递给一个函数。这是函数实现:

void
affiche_Tab2D(int *ptr, int n, int m)
{
    if (n>0 && m>0 && ptr!=NULL)
    {
        int (*lignePtr)[m]; // <-- Its my first time to see this declaration

        lignePtr = (int (*)[m]) ptr; // <-- and this too....

        for (int i = 0 ; i < n ; i++)
        {
            for (int j = 0 ; j < m ; j++) 
            {
                printf("%5d ",lignePtr[i][j]);
            }
            printf("\b\n");
        }
    }
}

Notice that ptr is 2D array but it uses a single pointer. Before, I used to pass 2D array using double pointers. In my main I have set up a 2D array (double pointer) and finding ways how to send it to that function.

请注意,ptr是2D数组,但它使用单个指针。之前,我曾经使用双指针传递2D数组。在我的主要内容中,我设置了一个2D数组(双指针)并找到了如何将其发送到该函数的方法。

Here are the function calls I tried which does not work:

这是我试过的函数调用,它不起作用:

int 
main(int argc, char * argv[]) 
{
    int ** numbers_table;
    int i, j;

    numbers_table = (int **)malloc(10 * sizeof(int *));

    for(i = 0; i < 10; i++)
        numbers_table[i] = (int *)malloc(10 * sizeof(int));

    for(i = 0; i < 10; i++)
        for(j = 0; j < 10; j++)
            numbers_table[i][j] = i + j;

    // Failed function call 1
    // affiche_Tab2D(numbers_table, 10, 10);

    // Failed function call 2
    // affiche_Tab2D(&numbers_table, 10, 10);

    for(i = 0; i < 10; i++)
    free(numbers_table[i]);

    free(numbers_table);

    return 0;
}

1 个解决方案

#1


2  

You can't pass numbers_table to your function because it is of type int ** while your function is expecting int *. Also you can't pass &numbers_table because it is of type int ***.
To pass a pointer to int to your function pass &numbers_table[0][0], having a type int *.

你不能将numbers_table传递给你的函数,因为它的类型是int **而你的函数期望int *。你也不能传递&numbers_table,因为它的类型是int ***。将指向int的指针传递给函数pass&numbers_table [0] [0],类型为int *。

affiche_Tab2D(&numbers_table[0][0], 10, 10);  

As per OP's comment:

根据OP的评论:

I would like to know more about the explanation of the declaration int (*lignePtr)[m] and lignePtr = (int (*)[m]) ptr; Its a bit confusing to understand.

我想了解更多关于声明int(* lignePtr)[m]和lignePtr =(int(*)[m])ptr的解释;理解它有点令人困惑。

int (*lingPtr)[m] is a pointer to an array (of integers) of m elements.
lignePtr = (int (*)[m]) ptr; is casting ptr to a pointer to array of m elements.

int(* lingPtr)[m]是指向m个元素的数组(整数)的指针。 lignePtr =(int(*)[m])ptr;将ptr转换为指向m个元素数组的指针。

#1


2  

You can't pass numbers_table to your function because it is of type int ** while your function is expecting int *. Also you can't pass &numbers_table because it is of type int ***.
To pass a pointer to int to your function pass &numbers_table[0][0], having a type int *.

你不能将numbers_table传递给你的函数,因为它的类型是int **而你的函数期望int *。你也不能传递&numbers_table,因为它的类型是int ***。将指向int的指针传递给函数pass&numbers_table [0] [0],类型为int *。

affiche_Tab2D(&numbers_table[0][0], 10, 10);  

As per OP's comment:

根据OP的评论:

I would like to know more about the explanation of the declaration int (*lignePtr)[m] and lignePtr = (int (*)[m]) ptr; Its a bit confusing to understand.

我想了解更多关于声明int(* lignePtr)[m]和lignePtr =(int(*)[m])ptr的解释;理解它有点令人困惑。

int (*lingPtr)[m] is a pointer to an array (of integers) of m elements.
lignePtr = (int (*)[m]) ptr; is casting ptr to a pointer to array of m elements.

int(* lingPtr)[m]是指向m个元素的数组(整数)的指针。 lignePtr =(int(*)[m])ptr;将ptr转换为指向m个元素数组的指针。