kosteg
kosteg

Reputation: 46

passing a two-dimmensional array to function

I am trying to compile the following simple code in Workbench:

 1. typedef float matrixType[3][3]
 2.     
 3. void my_func(matrixType matrix)
 4. {
 5.     printf("matrix[0][0] = %g\n",matrix[0][0]);
 6. }
 7. 
 8. void main()
 9. {
10.     matrixType my_matrix = {{0,1,2},{3,4,5},{6,7,8}};
11.     matrixType* ptr_matrix = &my_matrix;
12. 
13.     my_func(*ptr_matrix);
14. }

I receive the following warning:

test.c:13: warning: passing arg 1 of `my_func' from incompatible pointer type

I can't understand, what am I doing wrong. The compilation of the same code in Visual Studio works without any warnings, but in Workbench something is going wrong.

Thanks.

Upvotes: 1

Views: 168

Answers (2)

Gene
Gene

Reputation: 46960

With gcc (GCC) 4.5.3 with all warnings turned on it also compiles fine after making the following changes:

  1. Add a semicolon after the first line.
  2. Add #include <stdio.h> at top.
  3. Change the return type of main to int.
  4. Add return 0; as the last line.

The void main() is not correct C even though it appears in various books, manuals, and web tutorials. On some architectures it will cause strange problems, usually as the program terminates.

Taking the address of an array type is challenging the workbench type checker. I'm not going to drag out the C standard to figure out if the workbench warning is correct. It's probably a bug.

But I'm pretty sure that if you recode this way you will see no errors with any compiler:

#include <stdio.h>

typedef float rowType[3];
typedef rowType matrixType[3];

void my_func(matrixType matrix) 
{ 
   printf("matrix[0][0] = %g\n",matrix[0][0]); 
} 

int main() 
{ 
    matrixType my_matrix = {{0,1,2},{3,4,5},{6,7,8}}; 
    rowType* ptr_matrix = my_matrix; 
    my_func(ptr_matrix);
    return 0; 
} 

The reason is that my_matrix is automatically converted to a pointer to it's first element in the assignment

rowType* ptr_matrix = my_matrix; 

This is just as in

char s[] = "hello world!"; 
char *p = s;

the array name s is converted to a pointer to its first element.

The parameter in void my_func(matrixType matrix) has a type identical to rowType* because all arrays are also passed as pointers to first elements. So all the types in this code must match in a way that's very clearly defined in the C standard. &my_matrix may not be incorrect, but it's an "edge case" more likely to expose type checking bugs.

Upvotes: 1

Joseph Sheedy
Joseph Sheedy

Reputation: 6726

You are missing a semicolon at the end of line 1.

Upvotes: 1

Related Questions