This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Warning when using const pointer to fixed size array
- From: Aaron Rocha <hxdg21 at yahoo dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Mon, 30 Nov 2009 15:19:03 -0800 (PST)
- Subject: Warning when using const pointer to fixed size array
I am using :
$ gcc -dumpversion
4.2.4
$ gcc -dumpmachine
x86_64-linux-gnu
As expected, the following code does not generate any warnings when compiling with -Wall:
int main() {
int number = 0;
const int * const t = &number;
return *t;
}
However, this code does:
int main() {
int array[9] = {0};
const int (* const p)[9] = &array;
return (*p)[0];
}
$ gcc -Wall ./tst2.c
./tst2.c: In function âmainâ:
./tst2.c:7: warning: initialization from incompatible pointer type
In order to eliminate this warning, I have to declare array as:
const int array[9];
Why is this the case though? In both examples, I am trying to use a const pointer to access a non-const variable for read-only purposes. Isn't this supposed to be OK?
Thanks
__________________________________________________________________
Looking for the perfect gift? Give the gift of Flickr!
http://www.flickr.com/gift/