Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > const pointers

Reply
Thread Tools

const pointers

 
 
LBJ
Guest
Posts: n/a
 
      11-04-2003
I declare pointers as follows:

const int* pData; //pointer to read only data
int* const pData2; //read only pointer

Then I make the following assignment:

pData = pData2;

the read-only aspect of pData2 is not in any danger of being
corrupted by this assignment, and pData simply points to data that it
cannot modify. yet I am getting the following error on compilation:

warning: assignment discards qualifiers from pointer target type.

why would qualifiers be discarded?
 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      11-04-2003

"LBJ" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ...
> I declare pointers as follows:
>
> const int* pData; //pointer to read only data
> int* const pData2; //read only pointer
>
> Then I make the following assignment:
>
> pData = pData2;
>
> the read-only aspect of pData2 is not in any danger of being
> corrupted by this assignment, and pData simply points to data that it
> cannot modify. yet I am getting the following error on compilation:
>
> warning: assignment discards qualifiers from pointer target type.
>
> why would qualifiers be discarded?


Because they mean different things. IIRC "const int * p" means what p
points to is constant e.g. like "const void *" many C functions take [memcpy
for instance].

On the other hand "int * const p" means the pointer itself "p" is constant.

Why you get the error though is that they're not the same type so the
compiler can emit any warning it wants. In this case you lose the fact that
pdata2 is constant [and pdata] is not.

Tom


 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      11-04-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (LBJ) wrote:

> I declare pointers as follows:
>
> const int* pData; //pointer to read only data
> int* const pData2; //read only pointer
>
> Then I make the following assignment:
>
> pData = pData2;
>
> the read-only aspect of pData2 is not in any danger of being
> corrupted by this assignment, and pData simply points to data that it
> cannot modify. yet I am getting the following error on compilation:
>
> warning: assignment discards qualifiers from pointer target type.
>
> why would qualifiers be discarded?


Odd. I can see that a qualifier is discarded, but it's a qualifier for
the pointer type itself, not for [how I would interpret the phrase] the
pointer target type. And since you're not going to modify the original
pointer object by changing the copy of its value in pData, I see no
reason for the warning. Are you sure you didn't accidentally write the
assignment (or the declarations) the wrong way 'round?

Richard
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      11-04-2003
LBJ <(E-Mail Removed)> spoke thus:

> const int* pData; //pointer to read only data
> int* const pData2; //read only pointer
>
> Then I make the following assignment:
> pData = pData2;


> warning: assignment discards qualifiers from pointer target type.


FWIW (and believing one's compiler is at least a venial sin here...):

#include <stdio.h>

int main( void )
{
int a=3;
const int *p;
int* const p2=&a;
p=p2;
return 0;
}

/usr/bin/gcc -Wall -W -O2 -ansi -pedantic test.c

gives no messages whatsoever.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
LBJ
Guest
Posts: n/a
 
      11-04-2003
"Tom St Denis" <(E-Mail Removed)> wrote in message news:<XBQpb.182772$(E-Mail Removed) .cable.rogers.com>...
> "LBJ" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ...
> > I declare pointers as follows:
> >
> > const int* pData; //pointer to read only data
> > int* const pData2; //read only pointer
> >
> > Then I make the following assignment:
> >
> > pData = pData2;
> >
> > the read-only aspect of pData2 is not in any danger of being
> > corrupted by this assignment, and pData simply points to data that it
> > cannot modify. yet I am getting the following error on compilation:
> >
> > warning: assignment discards qualifiers from pointer target type.
> >
> > why would qualifiers be discarded?

>
> Because they mean different things.

however they mean two *independent* things, i.e. no conflict.

> IIRC "const int * p" means what p
> points to is constant e.g. like "const void *" many C functions take [memcpy
> for instance].
>
> On the other hand "int * const p" means the pointer itself "p" is constant.
>
> Why you get the error though is that they're not the same type so the
> compiler can emit any warning it wants. In this case you lose the fact that
> pdata2 is constant [and pdata] is not.
>
> Tom

 
Reply With Quote
 
LBJ
Guest
Posts: n/a
 
      11-04-2003
Christopher Benson-Manica <(E-Mail Removed)> wrote in message news:<bo8m8j$79h$(E-Mail Removed)>...
> LBJ <(E-Mail Removed)> spoke thus:
>
> > const int* pData; //pointer to read only data
> > int* const pData2; //read only pointer
> >
> > Then I make the following assignment:
> > pData = pData2;

>
> > warning: assignment discards qualifiers from pointer target type.

>
> FWIW (and believing one's compiler is at least a venial sin here...):
>
> #include <stdio.h>
>
> int main( void )
> {
> int a=3;
> const int *p;
> int* const p2=&a;
> p=p2;
> return 0;
> }
>
> /usr/bin/gcc -Wall -W -O2 -ansi -pedantic test.c
>
> gives no messages whatsoever.


thats good to hear. i believe gcc is a c++ compiler. i wonder what
the result would be with just a straight c compiler.
 
Reply With Quote
 
LBJ
Guest
Posts: n/a
 
      11-04-2003
Christopher Benson-Manica <(E-Mail Removed)> wrote in message news:<bo8m8j$79h$(E-Mail Removed)>...
> LBJ <(E-Mail Removed)> spoke thus:
>
> > const int* pData; //pointer to read only data
> > int* const pData2; //read only pointer
> >
> > Then I make the following assignment:
> > pData = pData2;

>
> > warning: assignment discards qualifiers from pointer target type.

>
> FWIW (and believing one's compiler is at least a venial sin here...):
>
> #include <stdio.h>
>
> int main( void )
> {
> int a=3;
> const int *p;
> int* const p2=&a;
> p=p2;
> return 0;
> }
>
> /usr/bin/gcc -Wall -W -O2 -ansi -pedantic test.c
>
> gives no messages whatsoever.


my mistake, i think gcc is the c compiler and g++ is the c++
compiler. but try to compile it with gcc again, except with the
-Wcast-qual option.
 
Reply With Quote
 
Sheldon Simms
Guest
Posts: n/a
 
      11-04-2003
On Tue, 04 Nov 2003 14:35:26 -0800, LBJ wrote:

> Christopher Benson-Manica <(E-Mail Removed)> wrote in message news:<bo8m8j$79h$(E-Mail Removed)>...
>> LBJ <(E-Mail Removed)> spoke thus:
>>
>> > const int* pData; //pointer to read only data
>> > int* const pData2; //read only pointer
>> >
>> > Then I make the following assignment:
>> > pData = pData2;

>>
>> > warning: assignment discards qualifiers from pointer target type.

>>
>> FWIW (and believing one's compiler is at least a venial sin here...):
>>
>> #include <stdio.h>
>>
>> int main( void )
>> {
>> int a=3;
>> const int *p;
>> int* const p2=&a;
>> p=p2;
>> return 0;
>> }
>>
>> /usr/bin/gcc -Wall -W -O2 -ansi -pedantic test.c
>>
>> gives no messages whatsoever.

>
> thats good to hear. i believe gcc is a c++ compiler. i wonder what
> the result would be with just a straight c compiler.


You believe wrong. Gcc invoked as above is a C compiler.

 
Reply With Quote
 
Mantorok Redgormor
Guest
Posts: n/a
 
      11-05-2003
(E-Mail Removed) (LBJ) wrote in message news:<(E-Mail Removed) m>...
> I declare pointers as follows:
>
> const int* pData; //pointer to read only data
> int* const pData2; //read only pointer
>
> Then I make the following assignment:
>
> pData = pData2;
>
> the read-only aspect of pData2 is not in any danger of being
> corrupted by this assignment, and pData simply points to data that it
> cannot modify. yet I am getting the following error on compilation:
>
> warning: assignment discards qualifiers from pointer target type.
>
> why would qualifiers be discarded?


because you are assigning to pData which doesn't have all the
qualifiers of pData2

besides which, i don't think assignment is allowed with a constant
expression but only through initialization


- nethlek
 
Reply With Quote
 
LBJ
Guest
Posts: n/a
 
      11-05-2003
Sheldon Simms <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> On Tue, 04 Nov 2003 14:35:26 -0800, LBJ wrote:
>
> > Christopher Benson-Manica <(E-Mail Removed)> wrote in message news:<bo8m8j$79h$(E-Mail Removed)>...
> >> LBJ <(E-Mail Removed)> spoke thus:
> >>
> >> > const int* pData; //pointer to read only data
> >> > int* const pData2; //read only pointer
> >> >
> >> > Then I make the following assignment:
> >> > pData = pData2;

>
> >> > warning: assignment discards qualifiers from pointer target type.
> >>
> >> FWIW (and believing one's compiler is at least a venial sin here...):
> >>
> >> #include <stdio.h>
> >>
> >> int main( void )
> >> {
> >> int a=3;
> >> const int *p;
> >> int* const p2=&a;
> >> p=p2;
> >> return 0;
> >> }
> >>
> >> /usr/bin/gcc -Wall -W -O2 -ansi -pedantic test.c
> >>
> >> gives no messages whatsoever.

> >
> > thats good to hear. i believe gcc is a c++ compiler. i wonder what
> > the result would be with just a straight c compiler.

>
> You believe wrong. Gcc invoked as above is a C compiler.


great, try it with -Wcast-qual
 
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