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.
With
gcc (GCC) 4.5.3with all warnings turned on it also compiles fine after making the following changes:#include <stdio.h>at top.maintoint.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:
The reason is that
my_matrixis automatically converted to a pointer to it’s first element in the assignmentThis is just as in
the array name
sis converted to a pointer to its first element.The parameter in
void my_func(matrixType matrix)has a type identical torowType*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_matrixmay not be incorrect, but it’s an “edge case” more likely to expose type checking bugs.