vicR vicR - 1 year ago 59
C Question

C return of local variable and wrong expected arguments for 4x4 matrix

I'm having a few problems to understandU/implement something small with pointers in c.
I'm having a 4x4 matrix and i want to transpose it. Its implementation is already working. but know i want to but the logic into methods to make it more fancy.

char arr[4][4] = { //filled }

The first code part only prints the array as i formated it and its working perfectly.

char matrixTranspose(char array[4][4]) {

char new_array[4][4];

// logic

return new_array;

So know I want to get back the the transposed matrix but I'm always getting back the warnings:

warning: function returns address of local variable

warning: return makes integer from pointer without a cast [enabledby

the further steps in my main method would be to just print the new array like:

char new_matrix = matrixTranspose(arr);

which results to the error:

warning: passing argument 1 of 'printArray' makes pointer from integer

without a cast [enabled by default] note: expected 'char (*)[4]' but
argument is of type 'char'

So my question is, why does it have to be a pointer and when do i use the pointer ? I jsut want to overgive the variable the whole way, transpose it give it back and print it, do i need pointes? thanks for any help.

Answer Source

A function should not return a non-static array declared inside it.

Change your main code to:

char arr1[4][4] = {...};
char arr2[4][4];


And your function to:

void matrixTranspose(const char input_arr[4][4],char output_arr[4][4])
    // logic
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download