typedef const char*

444 views
Skip to first unread message

Robert Wishlaw

unread,
Mar 14, 2003, 11:15:15 PM3/14/03
to
Are the following interchangeable?

typedef const char* X;

typedef const char * X;

typedef const char *X;

If so which is preferable?

Robert Wishlaw


Ben Pfaff

unread,
Mar 14, 2003, 11:20:45 PM3/14/03
to
"Robert Wishlaw" <rwis...@shaw.ca> writes:

> Are the following interchangeable?
>
> typedef const char* X;
>
> typedef const char * X;
>
> typedef const char *X;

Yes.

> If so which is preferable?

I prefer the last.
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_

rjh

unread,
Mar 15, 2003, 6:23:30 AM3/15/03
to
Ben Pfaff wrote:

> "Robert Wishlaw" <rwis...@shaw.ca> writes:
>
>> Are the following interchangeable?
>>
>> typedef const char* X;
>>
>> typedef const char * X;
>>
>> typedef const char *X;
>
> Yes.
>
>> If so which is preferable?
>
> I prefer the last.

So do I, but even more preferable is not to hide pointers inside typedefs
(IMHO, YMMV).

--
Richard Heathfield : bin...@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Benjamin Rutt

unread,
Mar 15, 2003, 4:42:23 PM3/15/03
to
"Robert Wishlaw" <rwis...@shaw.ca> writes:

> Are the following interchangeable?
>
> typedef const char* X;
>
> typedef const char * X;
>
> typedef const char *X;
>
> If so which is preferable?

It's already been answered that the above are interchangeable. But
since you're also soliciting opinions, I'll give mine.

I like the first two, since that way the type is at least separated
from the name by whitespace. Since type and name are two independent
things, I like the idea that they stay separate somehow.

However, many like the third form, and it is used in K&R.
--
Benjamin

Scott Fluhrer

unread,
Mar 15, 2003, 5:26:53 PM3/15/03
to

rjh <bin...@eton.powernet.co.uk> wrote in message
news:b4v2fi$919$2...@sparta.btinternet.com...

> Ben Pfaff wrote:
>
> > "Robert Wishlaw" <rwis...@shaw.ca> writes:
> >
> >> Are the following interchangeable?
> >>
> >> typedef const char* X;
> >>
> >> typedef const char * X;
> >>
> >> typedef const char *X;
> >
> > Yes.
> >
> >> If so which is preferable?
> >
> > I prefer the last.
>
> So do I, but even more preferable is not to hide pointers inside typedefs
> (IMHO, YMMV).

Actually, IMHO, whether it's preferable to hide pointers comes down to why
you're hiding pointers. If it's just so that you don't have to type 'const
char *pointer;' whenever you define a variable, then yes, it is rather,
errr, pointless. On the other hand, if you are defining a set of routines,
and you (for example) need a handle that has meaning to the routines, and
everything else should treat as an opaque object, then defining a typedef
for that handle is quite defensible, After all, it may be a 'const char *'
for now, but later on you may decide to make it something else, and making
it a typedef means that you need to change your routines, and the .h file
that defines the API into your routines, but nothing else (assuming, of
course, that everyone else played by the rules).

--
poncho

Arthur J. O'Dwyer

unread,
Mar 15, 2003, 5:29:12 PM3/15/03
to

On Sat, 15 Mar 2003, Benjamin Rutt wrote:
>
> "Robert Wishlaw" <rwis...@shaw.ca> writes:
> >
> > typedef const char* X;
> >
> > typedef const char * X;
> >
> > typedef const char *X;
>
> I like the first two, since that way the type is at least separated
> from the name by whitespace. Since type and name are two independent
> things, I like the idea that they stay separate somehow.

Warning! Warning!
If you think that the type and the name should "stay separate", I
assume you carefully avoid declaring multiple variables per line:

const char* X, Y; // One pointer and one 'const char'!

Or functions:

int (*foo)(); // How do you handle this sort of thing?

Or arrays:

int[] bob; // Only works in Java :-)

In my experience, if a type is too complex for your declaration
style to handle consistently, you're best off using a typedef for
it. Which *was* what the OP was asking about, I suppose...

typedef const char *X;

X x, y; // Two pointers, but how ugly is that?

-Arthur

Benjamin Rutt

unread,
Mar 15, 2003, 5:56:23 PM3/15/03
to
"Arthur J. O'Dwyer" <a...@andrew.cmu.edu> writes:

> Warning! Warning!
> If you think that the type and the name should "stay separate", I
> assume you carefully avoid declaring multiple variables per line:
>
> const char* X, Y; // One pointer and one 'const char'!

Yes, absolutely, I believe the above is error-prone and increases bugs
when new eyes modify your code. Of course, that's my opinion, based
on how I was taught by my coworkers.

> Or functions:
>
> int (*foo)(); // How do you handle this sort of thing?

I don't have a problem with the above.

> Or arrays:
>
> int[] bob; // Only works in Java :-)

Very clean separation of the type (int[]) and name (bob), but this is
OT here.

> In my experience, if a type is too complex for your declaration
> style to handle consistently, you're best off using a typedef for
> it. Which *was* what the OP was asking about, I suppose...
>
> typedef const char *X;
>
> X x, y; // Two pointers, but how ugly is that?

Very ugly. I'd write

const char * x;
const char * y;

instead, but that's just my opinion.
--
Benjamin

rjh

unread,
Mar 15, 2003, 6:06:11 PM3/15/03
to
Scott Fluhrer wrote:

>
> rjh <bin...@eton.powernet.co.uk> wrote in message
> news:b4v2fi$919$2...@sparta.btinternet.com...

<snip>

>> [...] even more preferable is not to hide pointers inside typedefs


>> (IMHO, YMMV).
>
> Actually, IMHO, whether it's preferable to hide pointers comes down to why
> you're hiding pointers. If it's just so that you don't have to type
> 'const char *pointer;' whenever you define a variable, then yes, it is
> rather,
> errr, pointless.

Well, it does save you some typing. This effort-saving was my original
reason for always typedef(f?)ing structs, and it remains the real reason,
although I now also have a good reason. :-)

> On the other hand, if you are defining a set of
> routines, and you (for example) need a handle that has meaning to the
> routines, and everything else should treat as an opaque object, then
> defining a typedef
> for that handle is quite defensible, After all, it may be a 'const char
> *' for now, but later on you may decide to make it something else, and
> making it a typedef means that you need to change your routines, and the
> .h file that defines the API into your routines, but nothing else
> (assuming, of course, that everyone else played by the rules).

This is a fair point well made, although I do think it remains useful to
keep pointers in plain view. To me at least, it is a helpful reminder that
"here be dragons".

E. Robert Tisdale

unread,
Mar 15, 2003, 6:08:40 PM3/15/03
to
Robert Wishlaw wrote:

> Are the following interchangeable?

Yes, except for documentation porposes.

> typedef const char* X;

X is a ponter to an object of type char.

> typedef const char * X;

I have no use for this form.

> typedef const char *X;

*X is a reference to an object of type char.

> If so which is preferable?

It's a matter of style.

Note that

const char* pX, Y, Z;

is the same as

const char *pX, Y, Z;

but different from

typedef char* pChar;
const pChar pX, Y, Z;


Kevin Easton

unread,
Mar 15, 2003, 7:38:57 PM3/15/03
to

Which has a whitespace argument too - it seperates the declaration
specifiers from the declarator.

- Kevin.

Emmanuel Delahaye

unread,
Mar 16, 2003, 5:19:08 AM3/16/03
to
In 'comp.lang.c', "Robert Wishlaw" <rwis...@shaw.ca> wrote:

> Are the following interchangeable?

Yes.



> typedef const char* X;
>
> typedef const char * X;
>
> typedef const char *X;
>
> If so which is preferable?

IMO, none of them, because we are C programmers and we are proud to use
pointers. OTH, I suggest that the pointer variables have a 'p' or 'p_'
prefix.

--
-ed- emdel at noos.fr ~]=[o
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/manuals/reader.aspx

"Say 'No' to the war!" http://www.votenowar.org/

Lorenzo Villari

unread,
Mar 22, 2003, 12:10:23 PM3/22/03
to

> > typedef const char *X;
>
> *X is a reference to an object of type char.
by reference you do mean pointer?

> Note that
>
> const char* pX, Y, Z;
>
> is the same as
>
> const char *pX, Y, Z;
>
> but different from
>
> typedef char* pChar;
> const pChar pX, Y, Z;

hmm...

- typedef char* pChar: "pChar" is a synonym for "char *"
- so "const pChar" should mean "const char *"...
- so "const pChar pX, Y, Z;" should read as "const char *pX, Y, Z;"...

Am I missing something or what?

rjh

unread,
Mar 22, 2003, 1:31:04 PM3/22/03
to
Lorenzo Villari wrote:

>
>> > typedef const char *X;
>>
>> *X is a reference to an object of type char.
> by reference you do mean pointer?
>
>> Note that
>>
>> const char* pX, Y, Z;
>>
>> is the same as
>>
>> const char *pX, Y, Z;
>>
>> but different from
>>
>> typedef char* pChar;
>> const pChar pX, Y, Z;
> hmm...
>
> - typedef char* pChar: "pChar" is a synonym for "char *"

Yes, objects of type pChar are objects of type char *

> - so "const pChar" should mean "const char *"...

Not at all. An object of type pChar is an object of type pointer to char.


> - so "const pChar pX, Y, Z;" should read as "const char *pX, Y, Z;"...

Nope. Think

const T x, y, z;

for some type T. All objects named here are of type const T.

typedef is not the same as #define!

>
> Am I missing something or what?

You are missing the fact that typedef is not the same as #define.

Kevin Easton

unread,
Mar 22, 2003, 8:18:26 PM3/22/03
to
Lorenzo Villari <vll...@tiscali.it> wrote:
>

You've over-snipped attributions, I've got no idea who wrote the lines
you quoted...

[...]


>> Note that
>>
>> const char* pX, Y, Z;
>>
>> is the same as
>>
>> const char *pX, Y, Z;
>>
>> but different from
>>
>> typedef char* pChar;
>> const pChar pX, Y, Z;
> hmm...
>
> - typedef char* pChar: "pChar" is a synonym for "char *"

pChar is a type name representing the type (char *).

> - so "const pChar" should mean "const char *"...

Nope. It means a const-qualified pChar.

> - so "const pChar pX, Y, Z;" should read as "const char *pX, Y, Z;"...
>
> Am I missing something or what?

Yep.

In this:

const char *pX, Y, Z;

You declare *pX, Y and Z as const-qualified char; thus pX is a
(non-const) pointer to const char, and X & Y are const chars.

In this:

const pChar pX, Y, Z;

You declare pX, Y, and Z as const-qualified pChar; thus pX, Y and Z are
all const pChars, which means const pointer to char.

The equivalent declaration without the typedef is:

char * const pX, * const Y, * const Z;

- Kevin.

nobody

unread,
Mar 22, 2003, 8:57:08 PM3/22/03
to
"E. Robert Tisdale" <E.Robert...@jpl.nasa.gov> wrote in message
news:3E73B278...@jpl.nasa.gov...

> Robert Wishlaw wrote:
>
> > Are the following interchangeable?
>
> Yes, except for documentation porposes.
>
> > typedef const char* X;
>
> X is a ponter to an object of type char.
>
That wasn't the question, and "answer" is wrong.
IMHO X is synonym for "pointer to constant character"
data type.

> > typedef const char * X;
>
> I have no use for this form.
>
> > typedef const char *X;
>
> *X is a reference to an object of type char.
>

If you said all 3 are interchangable, why did you give
different "description" here ? I'm not pretending to be
C guru, but I didn't hear yet about "object" or "reference"
in (standard) C language.
<snip>


Reply all
Reply to author
Forward
0 new messages