我有这个代码:
// *** 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/