const qualifier and pointers

Discussion in 'C Programming' started by Frédéric Kpama, Dec 28, 2011.

  1. 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)
    Frédéric Kpama, Dec 28, 2011
    #1
    1. Advertising

  2. Frédéric Kpama <> 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.
    Ben Bacarisse, Dec 28, 2011
    #2
    1. Advertising

  3. Frédéric Kpama <> 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://toerring.de
    Jens Thoms Toerring, Dec 28, 2011
    #3
  4. (Jens Thoms Toerring) writes:
    > Frédéric Kpama <> 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) <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"
    Keith Thompson, Dec 28, 2011
    #4
  5. Ok now I understand! thank you all for your answers.
    Frédéric Kpama, Dec 28, 2011
    #5
  6. Frédéric Kpama

    Kaz Kylheku Guest

    On 2011-12-28, Jens Thoms Toerring <> wrote:
    > Frédéric Kpama <> 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.
    Kaz Kylheku, Dec 29, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sergey Tolstov

    const qualifier and VC6.0

    Sergey Tolstov, Oct 7, 2003, in forum: C++
    Replies:
    8
    Views:
    471
    Howard
    Oct 7, 2003
  2. Javier
    Replies:
    2
    Views:
    558
    James Kanze
    Sep 4, 2007
  3. Ben Petering

    const type qualifier and external linkage (term.?)

    Ben Petering, Dec 30, 2007, in forum: C Programming
    Replies:
    7
    Views:
    443
    Thad Smith
    Dec 31, 2007
  4. Replies:
    12
    Views:
    806
    Old Wolf
    Dec 14, 2008
  5. paulo
    Replies:
    9
    Views:
    703
    James Kanze
    Mar 6, 2009
Loading...

Share This Page