简体   繁体   English

在C中制作二维数组结构时的指针类型不兼容

[英]incompatible pointer type in making 2D array struct in C

I'm trying to make 2D array struct in C like this in main function.我正在尝试在主函数中像这样在 C 中创建二维数组结构。

function_1(struct example** ex){}
void main(){
    struct example ex[num_1][num_2];
    function(ex);
}

But gcc keep telling me that type of ex and struct example** ex is different.但是 gcc 一直告诉我 ex 和 struct example** ex 的类型是不同的。 How can I solve this error?我该如何解决这个错误?

I edited my original answer:我编辑了我的原始答案:

You could instead write你可以改为写

function_1(struct example (*pex) [num_1][num_2]){}
void main(){
    struct example ex[num_1][num_2];
    function(& ex);
}

at least if you know num_1 and num_2 at compile time.至少如果您在编译时知道num_1num_2

A 2D array expression doesn't decay to a pointer to pointer, it decays to a pointer to an array .二维数组表达式不会衰减为指向指针的指针,而是衰减为指向数组的指针。

Unless it is the operand of the sizeof , _Alignof , or unary & operators, or is a string literal used initialize a character array in a declaration, an expression of type "N-element array of T " will be converted, or "decay", to an expression of type "pointer to T " and the value of the expression will be the address of the first element of the array.除非它是sizeof_Alignof或一元&运算符的操作数,或者是用于在声明中初始化字符数组的字符串文字,否则将转换类型为“ T的 N 元素数组”的表达式,或“衰减” , 指向“指向T的指针”类型的表达式,表达式的值将是数组第一个元素的地址。

In the call to function , the expression ex decays from type " num_1 -element array of num_2 -element array of struct example " to "pointer to num_2 -element array of struct example ", so your function prototype needs to be在对function的调用中,表达式ex从类型“ num_1 array of num_2 array of struct example ”衰减到“pointer to num_2 array of struct example ”,所以你的函数原型需要是

void function( struct example (*ex)[num_2] )
{
  ...
  ex[i][j] = some_value();
  ...
}

In the context of a function parameter declaration, T a[N] and T a[] are "adjusted" to T *a , so you could also write that prototype as在函数参数声明的上下文中, T a[N]T a[]被“调整”为T *a ,因此您也可以将该原型编写为

void function( struct example ex[][num_2] )
{
  ...
  ex[i][j] = some_value();
  ...
}

There are a couple of problems with this - function doesn't know how many rows are in the array, so you would need to pass that as a separate parameter:这有几个问题 - function不知道数组中有多少行,因此您需要将其作为单独的参数传递:

void function( struct example (*ex)[num_2], size_t rows )
{
  ...
}

Also the function can only accept arrays with num_2 columns - it cannot work on any arbitrarily-sized 2D array.此外,该函数只能接受具有num_2列的数组 - 它不能在任何任意大小的 2D 数组上工作。

If your function needs to handle arbitrarily-sized arrays and you have a compiler that supports variable-length arrays, you can do something like this:如果你的函数需要处理任意大小的数组并且你有一个支持可变长度数组的编译器,你可以这样做:

void function( size_t rows, size_t cols, struct example ex[rows][cols] )
{
  ...
}

and call it from main as并从main调用它

function( num_1, num_2, ex );

If your compiler doesn't support VLAs, you'll have to get creative.如果您的编译器不支持 VLA,则您必须发挥创造力。 One trick is to pass a pointer to the first element and treat it as a 1D array in the function:一个技巧是传递一个指向第一个元素的指针并将其视为函数中的一维数组:

void function( struct example *ex, size_t rows, size_t cols )
{
  ...
  ex[i * rows + j] = some_value();
  ...
}

and call it as并将其称为

function( &ex[0][0], num_1, num_2 );

Note that this trick only works for 2D arrays defined as请注意,此技巧仅适用于定义为的 2D 数组

T a[M][N];

or arrays dynamically allocated as或动态分配为的数组

T (*p)[N] = malloc( sizeof *p, M );

It won't work for 2D arrays allocated as它不适用于分配为的二维数组

T **p = malloc( sizeof *p * M );
for ( size_t i = 0; i < N; i++ )
  p[i] = malloc( sizeof *p[i], N );

but in that case, your function prototype would be但在这种情况下,您的函数原型将是

void function( T **p, size_t rows, size_t cols )
{
  ...
}

As a final note, main returns int , not void .最后一点, main返回int ,而不是void Older compilers won't complain, but the behavior is undefined and could cause problems at runtime.较旧的编译器不会抱怨,但行为是未定义的,可能会在运行时导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM