1

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.

Wug
  • 12,956
  • 4
  • 34
  • 54
kosteg
  • 46
  • 5
  • 3
    Probably not the problem, but you are missing a semicolon at the end of line 1. Besides that, the code looks actually fine to me. – betabandido Jul 19 '12 at 16:46

2 Answers2

1

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

Joseph Sheedy
  • 6,296
  • 4
  • 30
  • 31
1

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.

Gene
  • 46,253
  • 4
  • 58
  • 96
  • `void main()` and semicolon were not the issue here, but your suggestion really helped! Thanks a lot! Another problem I am dealing with is if my_func is defined this way: `void my_func(const matrixType matrix)`, I receive such a warning again. It looks strange to me, because I've thought it's ok to pass a non-const argument to a function that receives a 'const'. – kosteg Jul 22 '12 at 06:18
  • I mentioned `void main` because the editors chastised me once for letting something similar pass. No intent to be pedantic. It really is wrong and can bite you. The issue with `const` on array parameters and assignments is an old one. See for example http://stackoverflow.com/questions/4573349/c-function-const-multidimensional-array-argument-strange-warning. The only "automatic upgrade" to `const` that's allowed is forming a `const` pointer to a non-`const` object. Parameter passing and assignment aren't included in this. The `const`-ness has to match, or you have to use a cast to force it. – Gene Jul 22 '12 at 18:58
  • If this is the case, you should "Accept" my answer so that others who find this discussion later know it solved your problem. – Gene Jul 23 '12 at 18:44