0

I've a function like this (in a file file_name.c):

char function_name(multi_array[][10])
{
    /*change some character of multi_array*/

    return multi_array;
}

That takes multi_array, a multidimensional array of characters, changes some characters of the given parameter, and than returns multi_array modified. In main.c, i call the function like this:

multi_array_in_main = function_name(multi_array_in_main);

But the compiler gives me an error "icompatible type char[10][10] from type char"

What should i do? I'm not very confident with C so i don't know..!

4 Answers 4

1

You don't need to return anything.

Change:

char function_name(multi_array[][10])

To:

void function_name(multi_array[][10])

And your code should work fine (function_name will update whatever array it receives as an argument, as long as the dimensions are correct).

Sign up to request clarification or add additional context in comments.

Comments

0

Change the function to return void and remove the return statement. The array is actually passed as a pointer to it's first element, so any changes you make to it inside your function actually change the original object in the caller.

void function_name(multi_array[][10])
{
    /*change some character of multi_array*/
}

Comments

0

In your function header you declare function to return "char" type, but you return variable of char [][10], which is different type from the one in declaration (first line of your code).

Solution depends on what you really want to do. If you want to return that multiarray, change your function declaration. Also you defined parameter to be array of arrays, but it must be "array of array of char". Long story short, your declaration line should probably look like this:

char[][] function_name(char multi_array[][10])

Also, the changes made in multi_array made by this function will change multi_array even "outside" of the function and therefore you dont really need to return it. So you probably want to write this:

void function_name(char multi_array[][10])

Comments

0

As said, you do not need to return anything. The array is not copied, it is passed to your function as a pointer to the first element of the array. So, any element you change inside the function will be changed also outside because there is only one unique array.

Also if you insist, theoretically, it is possible to define a function returning a pointer to an array which is the closest thing to your original post. The declaration would be:

char (*function_name(char multi_array[][10]))[10] {
   ...
   return(multi_array);
}

It is so ugly, that you will probably prefer to define a new type for it:

typedef char (*multi_array_t)[10];

multi_array_t function_name(multi_array_t multi_array) {
   ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.