Need clarification on 'typedef' keyword

K

Krishna

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
 
V

Victor Bazarov

Krishna said:
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
 
B

BigBrian

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
 
K

Krishna

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. :)
 
V

Victor Bazarov

Krishna said:
[...] 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
 
H

Howard

Victor Bazarov said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top