Need clarification on 'typedef' keyword

Discussion in 'C++' started by Krishna, Apr 1, 2005.

  1. Krishna

    Krishna Guest

    I have three typedefs are defined below.

    typedef char* PCHAR;
    typedef const PCHAR PCCHAR1;
    typedef const char* PCCHAR2;

    In a piece of code, these typedef's are used as follows.

    PCCHAR1 pc1 = text;
    pc1 = text + 1;

    PCCHAR2 pc2 = text;
    pc2 = text + 1;

    Now assigning a value to pc1 gives a compile error, while assigning a value
    to pc2 doesn't give a compile error. The difference between the declaration
    of PCCHAR1 and PCCHAR2 is that i have substituted "PCHAR" with "char*". I
    had all along guessed that a typedef can be understood by just substituting.
    Can someone please clear up my confusion?

    Thanks & Regards,
    Krishna
    Krishna, Apr 1, 2005
    #1
    1. Advertising

  2. Krishna wrote:
    > I have three typedefs are defined below.
    >
    > typedef char* PCHAR;


    'PCHAR' is a synonym for a pointer to char.

    > typedef const PCHAR PCCHAR1;


    'PCCHAR1' is a synonym for a constant pointer to char.

    > typedef const char* PCCHAR2;


    'PCCHAR2' is a synonym for a pointer to a constant char.

    >
    > In a piece of code, these typedef's are used as follows.
    >
    > PCCHAR1 pc1 = text;
    > pc1 = text + 1;
    >
    > PCCHAR2 pc2 = text;
    > pc2 = text + 1;
    >
    > Now assigning a value to pc1 gives a compile error, while assigning a
    > value to pc2 doesn't give a compile error. The difference between the
    > declaration of PCCHAR1 and PCCHAR2 is that i have substituted "PCHAR"
    > with "char*". I had all along guessed that a typedef can be understood
    > by just substituting. Can someone please clear up my confusion?


    No, it cannot be understood by substituting like you did, it's not
    a macro. It is essential to see it as a synonym for a type-id especially
    WRT 'const' qualifier. 'const' relates to the object being defined. When
    you declare a typedef, the new name is like an object. So,

    typedef const BLAH CBLAH;

    is the same as

    typedef BLAH const CBLAH;

    Now substitute whatever BLAH means here, and what part does the 'const'
    affect? So, substitution analogy is only applicable if you place your
    cv-qualifiers in the proper place, *after* the type name.

    You do understand the difference between

    const char * something;

    and

    char * const something;

    , don't you?

    V
    Victor Bazarov, Apr 1, 2005
    #2
    1. Advertising

  3. Krishna

    BigBrian Guest

    The typedef for PCHAR1 is basically saying that PCHAR1 is a PCHAR that
    is const. The typedef for PCHAR2 is basically saying that PCHAR2 is
    a pointer to char that is const. Eventhough PCHAR is typdefed as a
    char *, the typedefs for PCHAR1 and PCHAR2 are not the same. PCHAR1 is
    a const PCHAR, and thus when you assign "text + 1" to it, the compiler
    gives an error. However, pc2 is a pointer to char* that's const, and
    thus "text + 1" can be assigned to it.

    if you replace the first typedef with #define PCHAR char*, it compiles.
    Thus, your believe that typedefs are the same as textual substitution
    is not valid.

    -Brian
    BigBrian, Apr 1, 2005
    #3
  4. Krishna

    Krishna Guest

    Victor wrote:

    > Krishna wrote:
    >
    >> I have three typedefs are defined below.
    >> typedef char* PCHAR;
    >> typedef const PCHAR PCCHAR1;
    >> typedef const char* PCCHAR2;
    >> The difference between the
    >> declaration of PCCHAR1 and PCCHAR2 is that i have substituted "PCHAR"
    >> with "char*". I had all along guessed that a typedef can be
    >> understood by just substituting. Can someone please clear up my
    >> confusion?


    > No, it cannot be understood by substituting like you did, it's not
    > a macro. It is essential to see it as a synonym for a type-id
    > especially WRT 'const' qualifier. 'const' relates to the object being

    defined.
    > When you declare a typedef, the new name is like an object.
    >So,
    > typedef const BLAH CBLAH;
    >
    > is the same as
    >
    > typedef BLAH const CBLAH;
    >
    > Now substitute whatever BLAH means here, and what part does the
    > 'const' affect? So, substitution analogy is only applicable if you
    > place your cv-qualifiers in the proper place, *after* the type name.


    That clears the confusion!
    So even though
    typedef const BLAH CBLAH;
    is syntactically correct, i guess its logically correct to always place the
    const qualifier after the type name. That should be a better programming
    practice?


    > You do understand the difference between
    > const char * something;
    > and
    > char * const something;
    > , don't you?

    I sure do. :)
    Krishna, Apr 1, 2005
    #4
  5. Krishna wrote:
    > [...] So even though typedef const BLAH CBLAH;
    > is syntactically correct, i guess its logically correct to always place
    > the const qualifier after the type name. That should be a better
    > programming practice?


    Well... I'm often on the fence regarding "better programming practices".

    For example, which is better
    char *pchar;
    or
    char* pchar;
    ?

    Here is another one: which is better
    for (int i = 0; i < somenumber; i++)
    or
    for (int i = 0; i < somenumber; ++i)

    The point is that for the beginner they might look different, while they
    are really not, OTOH, one can argue that they do promote some good habits
    in a novice. *I* use them interchangeably. Read: it doesn't matter where
    you place 'const' as long as you know what you're getting, and why, and as
    long as others (presumably knowledgeable C++ programmers as well) can see
    and understand what you meant/intended.

    Take this at its face value (since I am not paid to post here the value
    of my posts can be seen as 0.0000) and form your own opinion. Philosophy
    teaches us that in our heads we cannot have ideas that are not our own.
    That means that whatever you end up doing or thinking is produced by your
    own consciousness, your own mind. And you form those ideas based on the
    information available to you, and nobody can tell you what to think or do.
    Right?

    V
    Victor Bazarov, Apr 1, 2005
    #5
  6. Krishna

    Howard Guest

    "Victor Bazarov" <> wrote in message news:xlf3e.59016

    > Philosophy
    > teaches us that in our heads we cannot have ideas that are not our own.
    > That means that whatever you end up doing or thinking is produced by your
    > own consciousness, your own mind. And you form those ideas based on the
    > information available to you, and nobody can tell you what to think or do.
    > Right?
    >


    The voices in my head are screaming at me to disagree with you.

    :)

    Happy April Fool's Day!

    -Howard
    Howard, Apr 1, 2005
    #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. prem
    Replies:
    1
    Views:
    1,603
    Shankara Narayanan
    Nov 3, 2004
  2. HDSkiFreak
    Replies:
    2
    Views:
    3,532
    HDSkiFreak
    May 11, 2004
  3. Replies:
    6
    Views:
    449
    Peter Otten
    May 10, 2007
  4. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    349
  5. oor
    Replies:
    0
    Views:
    1,343
Loading...

Share This Page