将一个char指针数组传递给C中的函数?
我有以下代码:
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]
我试图了解问题所在。
  首先,你应该包含必要的头文件。  对于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使用以空字符结尾的字节字符串。  如果s1和s2都不包含空字节,则test的呼叫将导致分段错误: 
[1] 14940 segmentation fault (core dumped) ./a.out
  要么确保两者都包含空字节'
