简体   繁体   English

如何在C中传递函数多维char数组?

[英]How to pass in function a multidimensional char array in C?

This is my code and it is not working. 这是我的代码,无法正常工作。 It generate these errors when I pass char array like this grid[r][c] 当我像这样的grid[r][c]传递char数组时,它会生成这些错误grid[r][c]

[Error] use of parameter 'r' outside function body

[Error] use of parameter 'c' outside function body

It generate these errors when I pass char array like this grid[][c] 当我通过像char grid[][c]这样的char数组时,它会产生这些错误。

[Error] use of parameter 'c' outside function body

It generate these errors when I pass char array like this grid[][] 当我通过像char grid[][]这样的char数组时,它会产生这些错误。

[Error] declaration of 'grid' as multidimensional array must have bounds for all dimensions except the first 

And it runs perfectly fine when I pass this like this grid[1][2] ie just passing with an integer.0 当我像grid[1][2]这样传递它时,它运行得非常好,即仅传递一个integer.0

I am stuck here and I don't know what to do or what not?? 我被困在这里,不知道该怎么办?

How to get rid of this problem?? 如何摆脱这个问题? Help Me !!! 帮我 !!!

Thanks in Advance! 提前致谢!

void dfs(int r, int c, int pacman_r, int pacman_c, int food_r, int food_c, char grid[r][c]) {
    //logic here
}
int main(void) {
    int r, c;
    int pacman_r, pacman_c;
    int food_r, food_c;
    scanf( "%d %d", &pacman_r, &pacman_c);
    scanf( "%d %d", &food_r, &food_c);
    scanf( "%d %d", &r, &c);
    char grid[r][c];
    for( int i=0; i<r; i++) {
        scanf("%s[^\\n]%*c", grid[i]);
    }
    dfs( r, c, pacman_r, pacman_c, food_r, food_c, grid);
    return 0;
}

you should pass the argument as a char* then work with it as a pointer to a flattened instance of your array 您应该将参数作为char*传递,然后将其用作指向数组的扁平实例的指针

void fn(char* grid, int c){
    printf("%c", (grid+n*c)[m]);
}

this will print `grid[n][m] 这将显示`grid [n] [m]

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

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