public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Warning when using const pointer to fixed size array
@ 2009-11-30 23:19 Aaron Rocha
  2009-11-30 23:38 ` Thomas Martitz
  2009-12-01  9:52 ` Warning when using const pointer to fixed size array Andrew Haley
  0 siblings, 2 replies; 28+ messages in thread
From: Aaron Rocha @ 2009-11-30 23:19 UTC (permalink / raw)
  To: gcc-help

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/

^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2009-12-07  6:33 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-30 23:19 Warning when using const pointer to fixed size array Aaron Rocha
2009-11-30 23:38 ` Thomas Martitz
2009-11-30 23:47   ` me22
2009-12-01  0:05     ` Thomas Martitz
2009-12-01  0:08       ` Thomas Martitz
2009-12-01  0:08       ` me22
2009-12-01  0:24         ` Aaron Rocha
2009-12-01  0:44           ` me22
2009-12-01  2:29             ` One question about gcc fix_includes He Yunlong-B20256
2009-12-01  9:34               ` Andrew Haley
2009-12-02  2:37                 ` He Yunlong-B20256
2009-12-02  9:40                   ` Andrew Haley
2009-12-02  9:43               ` Kai Ruottu
2009-12-07  8:25                 ` He Yunlong-B20256
2009-12-01  9:52 ` Warning when using const pointer to fixed size array Andrew Haley
2009-12-01 14:54   ` Aaron Rocha
2009-12-01 15:08     ` Sergei Organov
2009-12-01 15:19       ` me22
2009-12-01 16:25         ` Sergei Organov
2009-12-01 16:29           ` me22
2009-12-01 16:42             ` Sergei Organov
2009-12-01 15:33     ` Andrew Haley
2009-12-01 15:43       ` Aaron Rocha
2009-12-01 16:22         ` Andrew Haley
2009-12-01 18:16           ` Aaron Rocha
2009-12-01 15:57     ` John (Eljay) Love-Jensen
2009-12-01 15:59       ` John (Eljay) Love-Jensen
2009-12-01 16:00       ` Andrew Haley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).