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

标签 c arrays pointers char

我有以下代码:

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’

This indicates that test() should have a char * as first argument. All in all your code should look like this:

#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

Either make sure that both contain a null byte '\0', or use strncmp and change the signature of 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))
*/

你的内存分配也是错误的。 arraychar** .您为 *array 分配内存这本身就是一个 char* .你永远不会为这个特定的指针分配内存,你错过了array[0] = malloc(n*sizeof(**array)) :

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

关于c - 将 char 指针数组传递给 C 中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11735734/

相关文章:

c - MPI 仅从 SELECT 处理器收集

c - 修改函数以返回指向字符串中单词最后一个字母的指针

c++ - 使用指向指针的指针

c - 显示一个数组,用指针排序,然后在 C 中再次显示原始数组

Javascript 随机引用 onclick

pointers - 映射的键作为结构体的指针? (用例)

c++ - 优化位操作以获取 NOT 值

对 strcpy 的这个实现感到困惑,为什么需要三个临时变量?

c++ - 如何让mini xml数据的呈现更具可读性?

arrays - 使用 map 或 slice 进行性能搜索