c - 将结构矩阵传递给 C 中的函数

标签 c matrix struct

我知道有类似的问题,但我仍然无法弄清楚,即使我已经阅读了两个小时。

struct box 
{ 
    char letter; 
    int occupied; //0 false, 1 true. 
}; 

void fill(struct box**, int, int, char*);  //ERROR HERE**


int main(int argc, char** argv) 
{ 
    int length=atoi(argv[4]), 
        width=atoi(argv[5]), 

    char alphabet[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    struct box soup[length][width]; 
    fill(soup, length, width, alphabet);  //HERE**
}

void fill(struct box soup[][], int length, int width, char*alphabet) //AND HERE**
{
   //omitted 
}

这些是我编译时遇到的错误:

warning: passing argument 1 of ‘fill’ from incompatible pointer type [enabled by default]  
     fill(soup, length, width, alphabet);  
     ^  

note: expected ‘struct box **’ but argument is of type ‘struct box (*)[(sizetype)(width)]’  
void fill(struct box **, int, int, char*);  
     ^  

error: array type has incomplete element type  
void fill(struct box soup[][], int length, int width, char*alphabet)
                     ^

我不明白为什么会失败,而我拥有的其他一些功能(如这个)确实有效:

void wordsToMemory(char**, char*);   //prototype
char* dictionary[Nwords];            
wordsToMemory(dictionary, argv[1]);  //calling the method
void wordsToMemory(char* dictionary[], char* argv) //function body
{
 //omitted
}

最佳答案

这将使其编译:

void fill(struct box** soup, int length, int width, char* alphabet)

void fill(struct box* soup[], int length, int width, char* alphabet)

使用[][]时,您会收到错误,因为没有从struct box*struct box的转换。

关于c - 将结构矩阵传递给 C 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20649181/

相关文章:

无法理解此 C 代码片段的输出

c++ - 逐像素复制 IPL 图像

c - 如何在不使用循环的情况下在 C 中初始化 N 维数组

c - 使用 Cuda 进行非方阵乘法

ios - 无法将结构附加到数组属性

c - 我的 for 循环不起作用?

c - 如何将标准输入读入字符串变量直到 C 中的 EOF?

r - 跨多个列表应用操作

c# - 动态结构失败

c++ - 如何加载知道第一个元素的结构?