By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

Need clarification on 'typedef' keyword

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Jul 23 '05 #2

P: n/a
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

Jul 23 '05 #3

P: n/a

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. :)
Jul 23 '05 #4

P: n/a
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
Jul 23 '05 #5

P: n/a

"Victor Bazarov" <v.********@comAcast.net> 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

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.