我有以下代码:
int main(){
char **array;
char a[5];
int n = 5;
array = malloc(n *sizeof *array);
/*Some code to assign array values*/
test(a, array);
return 0;
}
int test(char s1, char **s2){
if(strcmp(s1, s2[0]) != 0)
return 1;
return 0;
}
我正在尝试将char和char指针数组传递给函数,但上面的代码会导致以下错误和警告:
temp.c: In function ‘main’: temp.c:6:5: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration] temp.c:6:13: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default] temp.c:10:5: warning: implicit declaration of function ‘test’ [-Wimplicit-function-declaration] temp.c: At top level: temp.c:15:5: error: conflicting types for ‘test’ temp.c:15:1: note: an argument type that has a default promotion can’t match an empty parameter name list declaration temp.c:10:5: note: previous implicit declaration of ‘test’ was here temp.c: In function ‘test’: temp.c:16:5: warning: implicit declaration of function ‘strcmp’ [-Wimplicit-function-declaration]
我正在努力了解问题所在。
答案 0 :(得分:7)
首先,您应该包含必要的头文件。对于strcmp
,<string.h>
malloc
需要<malloc.h>
。此外,您至少需要在 main之前声明测试。如果这样做,您会注意到以下错误:
temp.c: In function ‘test’: temp.c:20:5: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [enabled by default] /usr/include/string.h:143:12: note: expected ‘const char *’ but argument is of type ‘char’
这表明test()
应该有char *
作为第一个参数。总而言之,您的代码应该如下所示:
#include <string.h> /* for strcmp */
#include <malloc.h> /* for malloc */
int test(char*,char**); /* added declaration */
int main(){
char **array;
char a[5];
int n = 5;
array = malloc(sizeof(*array));
array[0] = malloc(n * sizeof(**array));
/*Some code to assign array values*/
test(a, array);
free(*array); /* free the not longer needed memory */
free(array);
return 0;
}
int test(char * s1, char **s2){ /* changed to char* */
if(strcmp(s1, s2[0]) != 0) /* have a look at the comment after the code */
return 1;
return 0;
}
请注意strcmp
使用以null结尾的字节字符串。如果s1
和s2
都不包含空字节,则test
中的调用将导致分段错误:
[1] 14940 segmentation fault (core dumped) ./a.out
要么确保两者都包含空字节'\0'
,要么使用strncmp
并更改test
的签名:
int test(char * s1, char **s2, unsigned count){
if(strncmp(s1, s2[0], count) != 0)
return 1;
return 0;
}
/* don' forget to change the declaration to
int test(char*,char**,unsigned)
and call it with test(a,array,min(sizeof(a),n))
*/
你的内存分配也是错误的。 array
是char**
。您为*array
分配了内存,char*
本身就是array[0] = malloc(n*sizeof(**array))
。你永远不会为这个特定的指针分配内存,你错过了array = malloc(sizeof(*array));
*array = malloc(n * sizeof(**array));
:
{{1}}
答案 1 :(得分:3)
错误1
temp.c:6:13: warning: incompatible implicit declaration of
built-in function ‘malloc’ [enabled by default]
你的意思是?
array = malloc(n * sizeof(*array));
错误2
temp.c:15:5: error: conflicting types for ‘test’
temp.c:15:1: note: an argument type that has a default promotion can’t
match an empty parameter name list declaration
temp.c:10:5: note: previous implicit declaration of ‘test’ was here
您正在传递数组a
的第一个元素的地址:
test(a, array);
所以函数签名应该是:
int test(char* s1, char** s2)
答案 2 :(得分:3)
你有几个问题。首先是原型是错误的。传递给函数时,a
的数据类型会衰减为char 指针,因此您需要:
int test (char* s1, char** s2) { ... }
但是,即使您修复此问题,第一次使用时test
声明也不在范围内。您应该提供原型:
int test (char* s1, char** s2);
在main
之前,或者只是将整个定义(函数)移到main
之前。
此外,请不要忘记#include
string.h
和stdlib.h
标题,以便strcmp
和malloc
的原型也可用。
答案 3 :(得分:1)
当您将char数组传递给函数时,参数会衰减为指针。将函数参数更改为
int test(char* s1, char **s2);
^
^
并且您的代码至少应该编译