使int foo(int bar[][N])与int ** baz一起工作。

时间:2021-04-06 19:13:10

I have a function with the following prototype:

我有一个具有以下原型的函数:

int foo(int bar[][N]);

And I want to send to it the variable int ** baz which is an array of the same size, only it was allocated.

我想把变量int ** baz发送给它它是一个相同大小的数组,只分配了它。

I tried to explicitly cast and call foo() like so: foo((int(*)[N])baz); and it silenced the errors from the compiler but all the values in baz became junk when in foo() (why did it happen btw?).

我试图显式地转换和调用foo(),例如:foo(int(*)[N])baz;它消除了编译器的错误,但是在foo()中,baz中的所有值都变成了垃圾(为什么会发生btw?)

Is there a way to do this without duplicating the same function and changing it's prototype?

有没有一种方法可以不重复相同的函数并改变它的原型?

baz was allocated like so:

baz是这样分配的:

int ** baz = (int**)malloc(N*sizeof(int*));
    for (i = 0; i < N; i++)
        baz[i] = (int*)malloc(N*sizeof(int));

2 个解决方案

#1


3  

Allocate baz like

分配巴兹像

int ( *baz )[N] = malloc( N * N * sizeof( int ) );

In this case there is no problem to call function

在这种情况下,调用函数是没有问题的。

int foo(int bar[][N]);

with argument baz.

巴兹与论点。

Taking into account your comment then the function that allocates a two-dimensional array dynamically and returns a pointer to it can look like

考虑到您的注释,然后动态地分配一个二维数组的函数,并返回一个指向它的指针。

#include <stdio.h>
#include <stdlib.h>

#define N   10

typedef int ( *PTR )[N];

PTR allocate( int n );

int ( *allocate( int n ) )[N]
{
    int ( *p )[N] = malloc( n * sizeof( *p ) );

    return p;
}

int main( int argc, char * argv[] )
{
    free( allocate( N ) );

    return 0;
}

I showed two ways of the declaration of the function.

我展示了函数声明的两种方法。

#2


2  

The type int ** is not compatible with the type int (*)[N] (which is what the type of your function argument resolves to). The former is a pointer to pointer to int. The latter is a pointer to an array of N ints.

类型int **与类型int (*)[N]不兼容(这是您的函数参数的类型解析到的类型)。前者是指向int的指针,后者是指向数组N int的指针。

It is very important to understand at this point that arrays are not pointers. An array is a contiguous block of some number of elements of the same type. In most contexts, including when they appear as function arguments, the values of expressions having array type are automatically converted to pointers (to the array's first element), but that's a whole different thing than array objects being pointers. This distinction is a frequent source of confusion, but it is essential to understand when an array is a member of an aggregate type, such as a structure, union, or higher-dimensional array.

在这一点上理解数组不是指针是非常重要的。数组是同一类型的一些元素的连续块。在大多数情况下,包括当它们作为函数参数出现时,具有数组类型的表达式的值会自动转换为指针(指向数组的第一个元素),但这与数组对象作为指针完全不同。这种区别是常见的混淆来源,但是当数组是聚合类型的成员(例如结构、联合或高维数组)时,理解它是必要的。

If you want to dynamically allocate an M x N array that you can pass to your function, then that looks like this:

如果你想动态地分配一个mxn数组你可以传递给你的函数,那么它看起来是这样的:

int (*baz)[N] = malloc(M * sizeof(*baz));

If you want to pass your existing pointer pointer to your function, then the function should have this prototype:

如果您希望将现有的指针指针传递给函数,那么函数应该具有这个原型:

int foo(int **bar);

#1


3  

Allocate baz like

分配巴兹像

int ( *baz )[N] = malloc( N * N * sizeof( int ) );

In this case there is no problem to call function

在这种情况下,调用函数是没有问题的。

int foo(int bar[][N]);

with argument baz.

巴兹与论点。

Taking into account your comment then the function that allocates a two-dimensional array dynamically and returns a pointer to it can look like

考虑到您的注释,然后动态地分配一个二维数组的函数,并返回一个指向它的指针。

#include <stdio.h>
#include <stdlib.h>

#define N   10

typedef int ( *PTR )[N];

PTR allocate( int n );

int ( *allocate( int n ) )[N]
{
    int ( *p )[N] = malloc( n * sizeof( *p ) );

    return p;
}

int main( int argc, char * argv[] )
{
    free( allocate( N ) );

    return 0;
}

I showed two ways of the declaration of the function.

我展示了函数声明的两种方法。

#2


2  

The type int ** is not compatible with the type int (*)[N] (which is what the type of your function argument resolves to). The former is a pointer to pointer to int. The latter is a pointer to an array of N ints.

类型int **与类型int (*)[N]不兼容(这是您的函数参数的类型解析到的类型)。前者是指向int的指针,后者是指向数组N int的指针。

It is very important to understand at this point that arrays are not pointers. An array is a contiguous block of some number of elements of the same type. In most contexts, including when they appear as function arguments, the values of expressions having array type are automatically converted to pointers (to the array's first element), but that's a whole different thing than array objects being pointers. This distinction is a frequent source of confusion, but it is essential to understand when an array is a member of an aggregate type, such as a structure, union, or higher-dimensional array.

在这一点上理解数组不是指针是非常重要的。数组是同一类型的一些元素的连续块。在大多数情况下,包括当它们作为函数参数出现时,具有数组类型的表达式的值会自动转换为指针(指向数组的第一个元素),但这与数组对象作为指针完全不同。这种区别是常见的混淆来源,但是当数组是聚合类型的成员(例如结构、联合或高维数组)时,理解它是必要的。

If you want to dynamically allocate an M x N array that you can pass to your function, then that looks like this:

如果你想动态地分配一个mxn数组你可以传递给你的函数,那么它看起来是这样的:

int (*baz)[N] = malloc(M * sizeof(*baz));

If you want to pass your existing pointer pointer to your function, then the function should have this prototype:

如果您希望将现有的指针指针传递给函数,那么函数应该具有这个原型:

int foo(int **bar);