const qualifier and pointers

F

Frédéric Kpama

Hi,

I have to admit I don't fully understand all the implications of the
const qualifier. For example, I don't understand why a pointer to a
const stuff can not be compiled.

const char[] foo = "a string";
char * bar;

why can't I code bar = foo ? (If I remember, the gcc message was
pointing out some "dereference" restrictions, but I'm not really sure
I'm sorry)
 
B

Ben Bacarisse

Frédéric Kpama said:
I have to admit I don't fully understand all the implications of the
const qualifier. For example, I don't understand why a pointer to a
const stuff can not be compiled.

const char[] foo = "a string";
char * bar;

why can't I code bar = foo ?

The reason is that the assignment is "dangerous". C assumes that the
elements of foo are const for a reason and that you'd like to be told if
an assignment would make it pointless. The const tells the compiler to
complain if you write

foo[0] = 'A';

but after the assignment of foo to bar,

bar[0] = 'A';

will change foo[0] with no diagnostic required. The assignment bypasses
the const and is therefore considered a constraint violation (the
closest C comes to saying that some code is illegal). There are a few
rare cases where you need to bypass C's rules about what can be assigned
to what, and in those cases you can use a cast.

<snip>
 
J

Jens Thoms Toerring

Frédéric Kpama said:
I have to admit I don't fully understand all the implications of the
const qualifier. For example, I don't understand why a pointer to a
const stuff can not be compiled.
const char[] foo = "a string";

I guess this was supposed to be

const char foo[ ] = "a string";
char * bar;
why can't I code bar = foo ? (If I remember, the gcc message was
pointing out some "dereference" restrictions, but I'm not really sure
I'm sorry)

You can do that but you will receive a warning: "initialization discards
qualifiers from pointer target type". And it simply means that you
assign to a non-constant pointer - which you could use without any
further warnings to attempt to change the memory pointed to - from
a pointer to memory you are not allowed to modify. So the whole thing
is dangerous: by assigning 'foo' to 'bar' you now have a pointer that
you could try to mis-use for changing the memory at 'foo' which could
produce all kinds of weird results (not unlikely crashing your pro-
gram). So the compiler is telling you that you're doing something
potentially stupid.
Regards, Jens
 
K

Keith Thompson

Frédéric Kpama said:
I have to admit I don't fully understand all the implications of the
const qualifier. For example, I don't understand why a pointer to a
const stuff can not be compiled.
const char[] foo = "a string";

I guess this was supposed to be

const char foo[ ] = "a string";
char * bar;
why can't I code bar = foo ? (If I remember, the gcc message was
pointing out some "dereference" restrictions, but I'm not really sure
I'm sorry)

You can do that but you will receive a warning: "initialization discards
qualifiers from pointer target type".

Yes, gcc issues a mere warning by default, but that doesn't really
mean that "You can do that". The assignment violates a constraint,
which makes it just as "illegal" as a syntax error. The standard
only requires a diagnostic; it doesn't require the program to be
rejected. Other compilers, or gcc itself with different options
("-pedantic-errors") can treat the assignment as a fatal error.

[...]
 
K

Kaz Kylheku

Frédéric Kpama said:
I have to admit I don't fully understand all the implications of the
const qualifier. For example, I don't understand why a pointer to a
const stuff can not be compiled.
const char[] foo = "a string";

I guess this was supposed to be

const char foo[ ] = "a string";
char * bar;
why can't I code bar = foo ? (If I remember, the gcc message was
pointing out some "dereference" restrictions, but I'm not really sure
I'm sorry)

You can do that but you will receive a warning: "initialization discards
qualifiers from pointer target type".

That's how GCC words the error, but the wording is not specifically required.

A program which requires a diagnostic (due to a constraint rule violation
or syntax error) does not have to be translated.

So what's happening here is that GCC is defining the behavior of the program
(if anything is, that is). The standard isn't.

The initialization is permitted to occur, and after that, the program is GNU C,
not ISO C.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top