473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

const pointers

LBJ
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?
Nov 13 '05 #1
14 3259

"LBJ" <lb****@yahoo.com> wrote in message
news:62************************@posting.google.com ...
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
Nov 13 '05 #2
lb****@yahoo.com (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
Nov 13 '05 #3
LBJ <lb****@yahoo.com> 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.
Nov 13 '05 #4
LBJ
"Tom St Denis" <to********@iahu.ca> wrote in message news:<XB*******************@twister01.bloor.is.net .cable.rogers.com>...
"LBJ" <lb****@yahoo.com> wrote in message
news:62************************@posting.google.com ...
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

Nov 13 '05 #5
LBJ
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in message news:<bo**********@chessie.cirr.com>...
LBJ <lb****@yahoo.com> 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.
Nov 13 '05 #6
LBJ
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in message news:<bo**********@chessie.cirr.com>...
LBJ <lb****@yahoo.com> 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.
Nov 13 '05 #7
On Tue, 04 Nov 2003 14:35:26 -0800, LBJ wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in message news:<bo**********@chessie.cirr.com>...
LBJ <lb****@yahoo.com> 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.

Nov 13 '05 #8
lb****@yahoo.com (LBJ) wrote in message news:<62************************@posting.google.co 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
Nov 13 '05 #9
LBJ
Sheldon Simms <sh**********@yahoo.com> wrote in message news:<pa***************************@yahoo.com>...
On Tue, 04 Nov 2003 14:35:26 -0800, LBJ wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in message news:<bo**********@chessie.cirr.com>...
LBJ <lb****@yahoo.com> 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
Nov 13 '05 #10
LBJ <lb****@yahoo.com> spoke thus:
/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.


/usr/bin/gcc -Wall -W -O2 -ansi -pedantic -Wcast-qual still says nothing.
The compiler version is 3.3.1. I'm still waiting for someone authoritative to
state whether OP's observed behavior is legit or not... If it is, why is gcc
apparently allowed not to generate a diagnostic (without making it jump
through obscure command-line option hoops, at least)?

--
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.
Nov 13 '05 #11
On 2003-11-04, LBJ <lb****@yahoo.com> 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?


I am in agreement with Richard Bos. Your example is wrong.

Please compile the following snippet.

int main(void)
{
const int* pData;
int* const pData2;
pData = pData2;
return 0;
}

And see if you get a diagnostic. I do not get one at full warning
levels set (as I believe is correct).

-- James
Nov 13 '05 #12
On Wed, 05 Nov 2003 06:51:11 -0800, LBJ wrote:
Sheldon Simms <sh**********@yahoo.com> wrote in message news:<pa***************************@yahoo.com>...
On Tue, 04 Nov 2003 14:35:26 -0800, LBJ wrote:
> Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in message news:<bo**********@chessie.cirr.com>...
>> LBJ <lb****@yahoo.com> 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


Ok I tried it. Still no diagnostics.

Nov 13 '05 #13
In <bo**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
LBJ <lb****@yahoo.com> spoke thus:
/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.


/usr/bin/gcc -Wall -W -O2 -ansi -pedantic -Wcast-qual still says nothing.
The compiler version is 3.3.1. I'm still waiting for someone authoritative to
state whether OP's observed behavior is legit or not... If it is, why is gcc
apparently allowed not to generate a diagnostic (without making it jump
through obscure command-line option hoops, at least)?


It would have helped if you left the original code in place:

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

pData = pData2;

The relevant part of the standard (6.5.16.1p1 Simple assignment) is:

- both operands are pointers to qualified or unqualified versions
of compatible types, and the type pointed to by the left has
all the qualifiers of the type pointed to by the right;

The type pointed to by the left operand is const int and the type pointed
to by the right operand is plain int. Therefore, the above quoted
constraint is satisfied and no diagnostic is *required*. Otherwise,
the *only* options needed by gcc to emit the diagnostic would be
-ansi -pedantic.

OTOH, a compiler is free to diagnose anything it wants, including
perfectly legit code, so the compiler generating the misleading
diagnostic is still conforming, if the diagnostic has "warning" status
(i.e. the file is still correctly translated).

That's one of the most convoluted parts of the standard and there is no
wonder if some implementor didn't get it right.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #14
James Hu <jx*@despammed.com> wrote:
Please compile the following snippet.

int main(void)
{
const int* pData;
int* const pData2;
pData = pData2;
return 0;
}

And see if you get a diagnostic.


I get none. Oddly enough, because I'd have expected one about an
uninitialised pointer, but that's another warning altogether. I neither
expected nor got a warning about discarded qualifiers.

However, if I compile

int main(void)
{
const int* pData;
int* pData2; // Note removed "const".
pData2 = pData; // Note reversed assignment.
return 0;
}

I get almost exactly the warning the OP got:

Warning: assignment discards 'const' from pointer target type.

If I reinstate the const qualifier on pData2 itself, I also get a
warning about changing the value of a const object.

Richard
Nov 13 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too...
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
9
by: Alex | last post by:
Hi. I'll try my problem with this example: class C { protected: virtual int* getProtected(int index)=0; public: const int* get(int index) const { return (const int*) getProtected(index); } ...
5
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
23
by: grubertm | last post by:
When accessing pointer member variables these are not treated as const even though the instance is const. This came as a complete surprise to me and I would appreciate some feedback why that is the...
17
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer...
3
by: Jess | last post by:
Hello, If I have a constant array, i.e. it's elements aren't changed, should I declare it as: const int a = {1,2,3}; or int const a = {1,2,3}
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.