const pointers

Discussion in 'C Programming' started by LBJ, Nov 4, 2003.

  1. LBJ

    LBJ Guest

    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?
    LBJ, Nov 4, 2003
    #1
    1. Advertising

  2. LBJ

    Tom St Denis Guest

    "LBJ" <> wrote in message
    news:...
    > 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
    Tom St Denis, Nov 4, 2003
    #2
    1. Advertising

  3. LBJ

    Richard Bos Guest

    (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
    Richard Bos, Nov 4, 2003
    #3
  4. LBJ <> 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.
    Christopher Benson-Manica, Nov 4, 2003
    #4
  5. LBJ

    LBJ Guest

    "Tom St Denis" <> wrote in message news:<XBQpb.182772$>...
    > "LBJ" <> wrote in message
    > news:...
    > > 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
    LBJ, Nov 4, 2003
    #5
  6. LBJ

    LBJ Guest

    Christopher Benson-Manica <> wrote in message news:<bo8m8j$79h$>...
    > LBJ <> 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.
    LBJ, Nov 4, 2003
    #6
  7. LBJ

    LBJ Guest

    Christopher Benson-Manica <> wrote in message news:<bo8m8j$79h$>...
    > LBJ <> 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.
    LBJ, Nov 4, 2003
    #7
  8. On Tue, 04 Nov 2003 14:35:26 -0800, LBJ wrote:

    > Christopher Benson-Manica <> wrote in message news:<bo8m8j$79h$>...
    >> LBJ <> 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.
    Sheldon Simms, Nov 4, 2003
    #8
  9. (LBJ) wrote in message news:<>...
    > 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
    Mantorok Redgormor, Nov 5, 2003
    #9
  10. LBJ

    LBJ Guest

    Sheldon Simms <> wrote in message news:<>...
    > On Tue, 04 Nov 2003 14:35:26 -0800, LBJ wrote:
    >
    > > Christopher Benson-Manica <> wrote in message news:<bo8m8j$79h$>...
    > >> LBJ <> 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
    LBJ, Nov 5, 2003
    #10
  11. LBJ <> 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.
    Christopher Benson-Manica, Nov 5, 2003
    #11
  12. LBJ

    James Hu Guest

    On 2003-11-04, 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?


    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
    James Hu, Nov 5, 2003
    #12
  13. On Wed, 05 Nov 2003 06:51:11 -0800, LBJ wrote:

    > Sheldon Simms <> wrote in message news:<>...
    >> On Tue, 04 Nov 2003 14:35:26 -0800, LBJ wrote:
    >>
    >> > Christopher Benson-Manica <> wrote in message news:<bo8m8j$79h$>...
    >> >> LBJ <> 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.
    Sheldon Simms, Nov 5, 2003
    #13
  14. LBJ

    Dan Pop Guest

    In <bob641$eto$> Christopher Benson-Manica <> writes:

    >LBJ <> 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:
    Dan Pop, Nov 5, 2003
    #14
  15. LBJ

    Richard Bos Guest

    James Hu <> 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
    Richard Bos, Nov 6, 2003
    #15
    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. Replies:
    11
    Views:
    1,101
  2. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,115
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    887
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    541
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page