将char指针数组传递给C中的函数?

Pass a char pointer array to a function in C?

我有以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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指针数组传递给函数,但上面的代码会导致以下错误和警告:

1
2
3
4
5
6
7
8
9
10
temp.c: In function ‘main’:
temp.c:6:5: warning: implicit declaration of functionmalloc[-Wimplicit-function-declaration]
temp.c:6:13: warning: incompatible implicit declaration of built-in functionmalloc[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 functionstrcmp[-Wimplicit-function-declaration]

我想知道是什么问题。


首先,您应该包含必要的头文件。对于strcmp,您需要,对于malloc。另外,您至少需要在main之前声明测试。如果这样做,您将注意到以下错误:

1
2
3
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 *作为第一个论点。总之,您的代码应该如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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适用于以空结尾的字节字符串。如果s1s2都不包含空字节,test中的调用将导致分段错误:

1
[1]    14940 segmentation fault (core dumped)  ./a.out

要么确保两者都包含空字节'\0',要么使用strncmp并更改test的签名:

1
2
3
4
5
6
7
8
9
10
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))
*/

另外,您的内存分配错误。arraychar**。为*array分配内存,它本身就是char*。您从未为这个特定的指针分配内存,您缺少array[0] = malloc(n*sizeof(**array))

1
2
array = malloc(sizeof(*array));
*array = malloc(n * sizeof(**array));


你有几个问题。第一,原型是错误的。当传递给函数时,a的数据类型将衰减为char指针,因此需要:

1
int test (char* s1, char** s2) { ... }

但是,即使您修复了它,当您第一次使用它时,test声明也不在范围内。您应该提供一个原型:

1
int test (char* s1, char** s2);

main之前,或者简单地将整个定义(函数)移动到main之前。

另外,不要忘记#includestring.h头和stdlib.h头,这样strcmpmalloc的原型也可以使用。


错误1

1
2
temp.c:6:13: warning: incompatible implicit declaration of
built-in functionmalloc[enabled by default]

你是说这个吗?

1
array = malloc(n * sizeof(*array));

错误2

1
2
3
4
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的第一个元素的地址:

1
 test(a, array);

所以函数签名应该是:

1
int test(char* s1, char** s2)


当向函数传递一个char数组时,参数将衰减到指针。将函数参数更改为

1
2
3
 int test(char* s1, char **s2);
              ^
              ^

你的代码至少应该编译