【发布时间】:2017-10-18 06:51:54
【问题描述】:
我有这个代码:
// *** 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]){
^
我该如何解决?
【问题讨论】:
-
请告诉我们错误。
-
预期为 'char (*)[20]' 但参数类型为 'char *'
-
@uniopkn 这反过来会让你想到“
char*,我传递了一个数组指针。或者等等……也许我没有……”
标签: c function pointers parameter-passing