c - 如何在函数中传递参数数组指针?

标签 c function pointers parameter-passing

我有这个代码:

// *** foo.c ***

#include <stdlib.h> // malloc

typedef struct Node {
    char (*f)[20];
    //...
} Node;

int function(char f[30][20]){
    // Do some stuff with f...
}

int main(){
    Node * temp = (Node*)malloc(sizeof(Node));
    function(temp->f[20]);
}

我想将 temp->f 数组指针传递给函数,但在编译时出现以下错误:

$ gcc foo.c -o foo
foo.c: In function ‘main’:
foo.c:16:5: warning: passing argument 1 of ‘function’ from incompatible pointer type [enabled by default]
     function(temp->f[20]);
     ^
foo.c:10:5: note: expected ‘char (*)[20]’ but argument is of type ‘char *’
    int function(char f[30][20]){
        ^

我该如何解决?

最佳答案

expected 'char (*)[20]' but argument is of type 'char *

将调用函数的方式更改为:

function(temp->f);

还有:

关于c - 如何在函数中传递参数数组指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44048060/

相关文章:

c - 在 C 中将两个列表链接在一起

c - Unix 可执行文件 'Exec' ,十六进制转储显示 C 代码,而不是汇编

创建一个使用指针在 C 中转换 Pascal 字符串的方法

c - 字符指针预增

c - 将 2D 数组传递给函数时出现段错误

c - Unsigned int 未给出预期值

c - 如何在不传递 char ** 作为参数的情况下修改字符串值?

function - 如何使用函数在 Windows Powershell 中获取操作系统名称

c++ - 将指针数组传递给函数时遇到问题

Python:更改值 'in place' 的函数?