utarid
utarid

Reputation: 1715

passing char array as function parameters in C

I want to copy arr2 to arr and pass arr as a function paramater

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. 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

Upvotes: 1

Views: 4012

Answers (1)

P.P
P.P

Reputation: 121347

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. In pre-C99 mode, compiler would assume the function returns an int and this would cause problem. In C99 and C11, missing prototype makes your code invalid. So either declare the prototype at the top of the source file or move the function above main().

2) Include appropriate headers (<stdio.h> for printf etc).

Upvotes: 4

Related Questions