This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


 
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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]