Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strange warnings passing int* to const int*

Reply
Thread Tools

strange warnings passing int* to const int*

 
 
george doubleu
Guest
Posts: n/a
 
      07-09-2004
hi,

i'm using "gcc version 3.3.3 (Debian)" to compile the program pasted below.
I get the two warnings you can see in the remarks. The second warning is
perfectly OK for me, but the first one I don't understand.

Isn't the "const int *" construct in the signature of func1 a hint for the
user, that func1 doesn't modify **arg? Why then is it dangerous to
pass an alterable argmument?

thanks for your help!

----------------------------------------------------
void func1(const int *arg[]) { }

void func2(int *arg[]) { }

int main(argc)
{
const int *ary1[10];
int *ary2[10];

func1(ary1);
func1(ary2); // warning: passing arg 1 of `func1' from incompatible pointer type
func2(ary1); // warning: passing arg 1 of `func2' from incompatible pointer type
func2(ary2);
}
----------------------------------------------------
 
Reply With Quote
 
 
 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      07-09-2004

On Fri, 9 Jul 2004, george doubleu wrote:
>
> i'm using "gcc version 3.3.3 (Debian)" to compile the program pasted below.
> I get the two warnings you can see in the remarks. The second warning is
> perfectly OK for me, but the first one I don't understand.
>
> Isn't the "const int *" construct in the signature of func1 a hint for the
> user, that func1 doesn't modify **arg? Why then is it dangerous to
> pass an alterable argument?



> void func1(const int *arg[]) { }
>
> int *ary2[10];
>
> func1(ary2); // warning: passing arg 1 of `func1'
> // from incompatible pointer type



'ary2' is an 'array[10] of pointer to int'. It decays in this context
to a 'pointer to pointer to int'. That is, it is the address of a
'pointer to int'.

'func1' expects a parameter of type 'pointer to pointer to const int'.
That is, it expects the address of a 'pointer to const int'.

'pointer to int' and 'pointer to const int' are two different
types, in C. Pointers to different types are not compatible. Thus
'pointer to (pointer to int)' is not compatible with 'pointer to
(pointer to const int)'. Thus GCC gives you a warning.

How to fix it? See another thread in this newsgroup about
improper const-ifying's leading to type errors (Passing const void*
to free, or some such). Also Google for "const poisoning."
The bottom line is that this kind of error can look bizarre, but
it never occurs in well-written, real-life code, so just accept
it and you'll never see it again.

-Arthur
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments