通过一个char指针数组在C函数? [英] Pass a char pointer array to a function in C?

查看:267
本文介绍了通过一个char指针数组在C函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

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和字符指针数组传递给一个函数,但上面code会导致以下错误和警告:

I'm trying to pass char and char pointer array to a function, but the above code results in the following errors and warnings:

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]

隐式声明

我想知道是什么问题。

I'm trying to understand what the problem is.

推荐答案

首先,你应该包括必要的头文件。对于 STRCMP 需要<文件string.h> 的malloc <&malloc.h所GT; 。另外你需要的的主至少申报检验。如果你这样做,你会发现以下错误:

First of all, you should include the necessary header files. For strcmp you need <string.h>, for malloc <malloc.h>. Also you need to at least declare test before main. If you do this you'll notice the following error:

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’

这表明测试()应该有一个的char * 作为第一个参数。所有的一切你code应该是这样的:

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 与空值终止字节串的作品。如果没有 S1 也不 S2 包含空字节呼叫测试将导致分段错误:

Edit

Please notice that strcmp works with null-terminated byte strings. If neither s1 nor s2 contain a null byte the call in test will result in a segmentation fault:

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

要么确保这两个包含空字节'\\ 0',或使用的 STRNCMP 并更改测试的签名

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))
*/

此外,您的内存分配是错误的。 阵列的char ** 。您分配给存储阵列* 这本身就是一个的char * 。你从来不为这个特定指针分配内存,你错过数组[0] =的malloc(N * sizeof的(**阵列))

Also your allocation of memory is wrong. array is a char**. You allocate memory for *array which is itself a char*. You never allocate memory for this specific pointer, you're missing array[0] = malloc(n*sizeof(**array)):

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

这篇关于通过一个char指针数组在C函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆