strange warnings passing int* to const int*

G

george doubleu

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);
}
----------------------------------------------------
 
A

Arthur J. O'Dwyer

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top