【发布时间】:2012-07-28 23:04:11
【问题描述】:
我有以下代码:
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:在函数“main”中: temp.c:6:5:警告:函数“malloc”的隐式声明 [-Wimplicit-function-declaration] temp.c:6:13:警告:内置函数“malloc”的隐式声明不兼容 [默认启用] temp.c:10:5:警告:函数“测试”的隐式声明 [-Wimplicit-function-declaration] temp.c:在顶层: temp.c:15:5:错误:“测试”的类型冲突 temp.c:15:1:注意:具有默认提升的参数类型不能匹配空参数名称列表声明 temp.c:10:5:注意:先前的“测试”隐式声明在这里 temp.c:在函数“测试”中: temp.c:16:5:警告:函数‘strcmp’的隐式声明 [-Wimplicit-function-declaration]
我正在尝试了解问题所在。
【问题讨论】:
-
s1只是一个字符。test的原型应该是int test(char* s1, char **s2) -
始终包含错误 - 它们很重要,如果我们也可以向您解释它们,那么您将能够自己改进未来的代码。
-
编译所有警告并将它们视为错误(Witg gcc,
-Wall --Werror)。在这种情况下它会捕获错误。