Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

const qualifier and pointers

 
 
Frédéric Kpama
Guest
Posts: n/a
 
      12-28-2011
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)


 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-28-2011
Frédéric Kpama <(E-Mail Removed)> writes:

> 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>
--
Ben.
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      12-28-2011
Frédéric Kpama <(E-Mail Removed)> wrote:
> 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
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-28-2011
(E-Mail Removed) (Jens Thoms Toerring) writes:
> Frédéric Kpama <(E-Mail Removed)> wrote:
>> 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.

[...]

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Frédéric Kpama
Guest
Posts: n/a
 
      12-28-2011
Ok now I understand! thank you all for your answers.
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      12-29-2011
On 2011-12-28, Jens Thoms Toerring <(E-Mail Removed)> wrote:
> Frédéric Kpama <(E-Mail Removed)> wrote:
>> 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.
 
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
Constant qualifier pros/cons, type const& versus const type&, etc. paulo C++ 9 03-06-2009 09:55 AM
type compatible and const qualifier nicolas.sitbon@gmail.com C Programming 12 12-14-2008 07:39 AM
const type qualifier and external linkage (term.?) Ben Petering C Programming 7 12-31-2007 05:01 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
const qualifier and VC6.0 Sergey Tolstov C++ 8 10-07-2003 02:42 PM



Advertisments