Pointers, typedef's and const's

Discussion in 'C Programming' started by Alex, May 1, 2008.

  1. Alex

    Alex Guest

    The code below can't be compiled:

    typedef char* POINTER;
    const POINTER ptr;
    ptr++;

    The compiler (Sun C 5.8 Patch 121015-04 2007/01/10) complains:

    "test.c", line ...: operand must be modifiable lvalue: op "++"

    It looks like the "const POINTER" has been compiled to "char* const".
    A question is - why?

    Best wishes,
    Alex
     
    Alex, May 1, 2008
    #1
    1. Advertising

  2. On May 1, 5:00 pm, Alex <> wrote:
    > The code below can't be compiled:
    >
    > typedef char* POINTER;
    > const POINTER ptr;
    > ptr++;
    >
    > The compiler (Sun C 5.8 Patch 121015-04 2007/01/10) complains:
    >
    > "test.c", line ...: operand must be modifiable lvalue: op "++"
    >
    > It looks like the "const POINTER" has been compiled to "char* const".
    > A question is - why?
    >
    > Best wishes,
    > Alex


    <http://c-faq.com/> Question 11.11, <http://c-faq.com/ansi/
    typedefconst.html>.

    --
    Robert Gamble
     
    Robert Gamble, May 1, 2008
    #2
    1. Advertising

  3. Alex wrote:
    > The code below can't be compiled:
    >
    > typedef char* POINTER;
    > const POINTER ptr;
    > ptr++;
    >
    > The compiler (Sun C 5.8 Patch 121015-04 2007/01/10) complains:
    >
    > "test.c", line ...: operand must be modifiable lvalue: op "++"
    >
    > It looks like the "const POINTER" has been compiled to "char* const".
    > A question is - why?


    Why not???

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, May 2, 2008
    #3
  4. Alex

    Alex Guest

    On May 1, 5:04 pm, Robert Gamble <> wrote:
    > On May 1, 5:00 pm, Alex <> wrote:
    >
    > > The code below can't be compiled:

    >
    > > typedef char* POINTER;
    > > const POINTER ptr;
    > > ptr++;

    >
    > > The compiler (Sun C 5.8 Patch 121015-04 2007/01/10) complains:

    >
    > > "test.c", line ...: operand must be modifiable lvalue: op "++"

    >
    > > It looks like the "const POINTER" has been compiled to "char* const".
    > > A question is - why?

    >
    > > Best wishes,
    > > Alex

    >
    > <http://c-faq.com/> Question 11.11, <http://c-faq.com/ansi/
    > typedefconst.html>.
    >
    > --
    > Robert Gamble


    Robert, thanks

    From the FAQ 11.11: "The typedef'ed declaration of p does not ``look
    inside'' the typedef to see that there is a pointer involved."

    So meanings of "const POINTER" and "POINTER const" are the same... It
    looks counterintuitive to me.

    Do you know any reason, why it has been done this way? Was it too
    difficult to look inside the typedef? I don't think so - pointer
    dereferencing works, right?

    Best wishes,
    Alex
     
    Alex, May 2, 2008
    #4
  5. Alex wrote:
    >
    > From the FAQ 11.11: "The typedef'ed declaration of p does not ``look
    > inside'' the typedef to see that there is a pointer involved."
    >
    > So meanings of "const POINTER" and "POINTER const" are the same... It
    > looks counterintuitive to me.


    It looks counterintuitive to you probably because you think of
    typedef-names as macros, while in reality they are not macros, but
    alternative names for types.

    Meaning of "const POINTER" and "POINTER const" is the same just like the
    meaning of "const int" is the same as that of "int const".

    The current behavior qualifiers applied to typedef-names is perfectly
    intuitive, once you get the proper understanding of what typedef-names are.

    > Do you know any reason, why it has been done this way? Was it too
    > difficult to look inside the typedef?


    The real question in this case is why would anyone even want to "look
    inside the typedef" as you suggest.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, May 2, 2008
    #5
  6. Alex

    Alex Guest

    On May 1, 10:17 pm, Jack Klein <> wrote:
    > On Thu, 1 May 2008 14:00:46 -0700 (PDT), Alex <>
    > wrote in comp.lang.c:
    >
    > > The code below can't be compiled:

    >
    > > typedef char* POINTER;
    > > const POINTER ptr;
    > > ptr++;

    >
    > > The compiler (Sun C 5.8 Patch 121015-04 2007/01/10) complains:

    >
    > > "test.c", line ...: operand must be modifiable lvalue: op "++"

    >
    > > It looks like the "const POINTER" has been compiled to "char* const".
    > > A question is - why?

    >
    > > Best wishes,
    > > Alex

    >
    > That is just one of the reasons why hiding a pointer in a typedef is a
    > mistake in 99% of the cases where you see it.
    >
    > Why do you want to use a typedef instead of using "char *"? What do
    > you think that you gain?
    >
    > --
    > Jack Klein
    > Home:http://JK-Technology.Com
    > FAQs for
    > comp.lang.chttp://c-faq.com/
    > comp.lang.c++http://www.parashift.com/c -faq-lite/
    > alt.comp.lang.learn.c-c++http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


    Well, I don't use a typedef instead of "char *". I just was annoyed by
    chains of asterisks, caused by "pointer to pointer to pointer"
    declarations and tried to improve my code readability.

    I tend to consider typedef's as user-defined types - that's why I
    never write "char *ch", but "char* ch" instead (and I don't declare
    many variables in one statement). I expected my new types to behave
    like predefined types, including a possibility to "constanize" them.
    May be, this two-face functionality of "const" for pointers is a real
    evil here. I'd prefer to write something like this:

    (const char)* const ptr;

    But it's not C - right? I like the "const" keyword and use it A LOT,
    so one more heretical thought - all the variables should be "const" by
    default, and if you need a non-const variable, you'll have to declare
    that fact with some keyword.

    Best wishes,
    Alex
     
    Alex, May 2, 2008
    #6
  7. Alex

    John Bode Guest

    On May 1, 4:00 pm, Alex <> wrote:
    > The code below can't be compiled:
    >
    > typedef char* POINTER;
    > const POINTER ptr;
    > ptr++;
    >
    > The compiler (Sun C 5.8 Patch 121015-04 2007/01/10) complains:
    >
    > "test.c", line ...: operand must be modifiable lvalue: op "++"
    >
    > It looks like the "const POINTER" has been compiled to "char* const".
    > A question is - why?
    >
    > Best wishes,
    > Alex


    Because you've created a synonym for a pointer type, and you're
    applying the const qualifier to the synonym; i.e., you've said you
    want the pointer value to be constant, not the thing being pointed
    to.
     
    John Bode, May 2, 2008
    #7
    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,125
  2. Javier
    Replies:
    2
    Views:
    585
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,146
    Tim Rentsch
    Nov 10, 2008
  4. Replies:
    4
    Views:
    2,667
  5. Vladimir Menshakov
    Replies:
    1
    Views:
    371
Loading...

Share This Page