const pointers

L

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?
 
T

Tom St Denis

LBJ said:
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
 
R

Richard Bos

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
 
C

Christopher Benson-Manica

LBJ said:
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.
 
L

LBJ

Tom St Denis said:
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
 
L

LBJ

Christopher Benson-Manica said:
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.
 
L

LBJ

Christopher Benson-Manica said:
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.
 
S

Sheldon Simms

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.
 
M

Mantorok Redgormor

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
 
C

Christopher Benson-Manica

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)?
 
J

James Hu

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
 
D

Dan Pop

In said:
/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
 
R

Richard Bos

James Hu said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top