473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need clarification on 'typedef' keyword

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
5 1718
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
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

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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Anonymous | last post by:
I'm trying to port code from VC.net to VC.net 2003. Here's a typical piece of code that's often used: template<class T> class PseudoContainer { typedef T::iterator iterator; // this line will...
3
by: ma740988 | last post by:
Consider the 'C' source. void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal) { doorBellDetected = doorBellVal; } void slRcv() { starLinkOpenStruct myOpenStruct;
8
by: J Krugman | last post by:
My compiler complains if I do something like this typedef struct { node *next; } node; but it's OK with typedef struct superfluous_identifier { superfluous_identifier *next;
2
by: Mehta Shailendrakumar | last post by:
Hi all, Have alook at two different declarations below: typedef unsigned char uc; typedef void (*fptr) (void); The first declaration has a left part which is a C keyword: "unsigned char"...
134
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
1
by: sophia | last post by:
Dear all, the following are the differences b/w #define and typedef ,which i have seen in Peter van der lindens book. is there any other difference between thes two ? The right way to...
11
by: copx | last post by:
For some reason I cannot add a const qualifier to a typedefed pointer type if said type is used for a return value. It does work if the type is used for a parameter. I do not see the logic behind...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.