简体   繁体   English

在C中将char数组作为函数参数传递

[英]passing char array as function parameters in C

I want to copy arr2 to arr and pass arr as a function paramater 我想将arr2复制到arr并将arr作为函数参数传递

void func(char * array)
{}

int main(void)
{
    int a;
    char arr[6][50];
    char arr2[][50]={"qweeeaa","bbbb","ffaa","eeaa","aaaa","ffaa"};

    for(a=0; a<6;a++)
    {
        strcpy(arr[a], arr2[a]);
    }

    func(arr);

    return 0;
}

But I can not pass arr as a function parameter. 但是我不能将arr作为函数参数传递。 I get 我懂了

[Warning] passing argument 1 of 'func' from incompatible pointer type [enabled by default]
[Note] expected 'char *' but argument is of type 'char (*)[50]'

I am using MinGW GCC 4.8.1 我正在使用MinGW GCC 4.8.1

The type you pass and the type the function expects do not match. 您传递的类型和函数期望的类型不匹配。 Change the function to receive a pointer to an array: 更改函数以接收指向数组的指针:

void func(char (*array)[50])
{

}

Other problems: 其他问题:

1) You haven't declared a prototype for func() either. 1)您也没有为func()声明原型。 In pre-C99 mode, compiler would assume the function returns an int and this would cause problem. 在C99之前的模式下,编译器将假定函数返回一个int ,这将导致问题。 In C99 and C11, missing prototype makes your code invalid. 在C99和C11中,缺少原型会使您的代码无效。 So either declare the prototype at the top of the source file or move the function above main() . 因此,要么在源文件的顶部声明原型,要么将函数移到main()上方。

2) Include appropriate headers ( <stdio.h> for printf etc). 2)包括适当的头文件(对于printf等, <stdio.h> )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM