Passing (typedef_name *) args into (typedef_name const *) params produces warning if typedef_name is an array type

Hamzah Nassif hamzahnassif@yahoo.com
Thu Jan 24 06:29:00 GMT 2008


 
gcc produces a warning if an argument of type "pointer to array type" was passed into a parameter of type "pointer to same array type const", while the same doesn't happen if the referenced type was not an array type!



Passing pointer args to "pointer to const" params is quite common especially in the case of string manipulation routines (often declared with "char const *" parameters and passed in "char *" arguments). This is actually very desirable behavior. However, I cannot currently do the same when dealing with pointers to array type objects without getting a warning.



Here's a small sample that illustrates this. The sample code does both cases, but I only get one warning for the case of passing "var1" into the parameter of "function1", not for passing "var2" into the parameter of "function2".



The only difference between the two cases is the typedef declaration at the very top of the file.



SAMPLE CODE

-----------

typedef unsigned long type1[2];

typedef unsigned long long type2;



unsigned char function1(type1 const *ptr)

{

  return ptr != 0;

}



unsigned char function2(type2 const *ptr)

{

  return ptr != 0;

}



void test(void)

{

  type1 var1;

  type2 var2;

  unsigned char ret;



  ret = function1(&var1);

  ret = function2(&var2);

}



GCC COMMAND

-----------

> gcc -c testgcc.c



GCC OUTPUT

 ----------

testgcc.c: In function `test':

 testgcc.c:20: warning: passing arg 1 of `function1' from incompatible pointer type

 



It seems to me like both cases should be fine, shouldn't they?



Any explanation would be much appreciated.



Thanks,



Hamzah







      ____________________________________________________________________________________
Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  http://tools.search.yahoo.com/newsearch/category.php?category=shopping



More information about the Gcc-help mailing list