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]

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/


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