Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pointer question

0 views
Skip to first unread message

Frederic Martinelli

unread,
Dec 24, 1998, 3:00:00 AM12/24/98
to
Hello,

i'm coding C since quite some time now, but i never get reply to a
simple (?) question, in books nor over the Net : Can someone explain
me as deep as it's possible (even asm level related i.e. how the
compiler handle those cases) what are the differences between :

type *foo;
type* foo;
type * foo;

Thanks for any light you may cast on that mystery ! :)
Merry Xmas,
Fred.
PS: I'm using gcc but i bet the reply should be pretty universal.
--
mailto:aer...@club-internet.fr

Ben Pfaff

unread,
Dec 24, 1998, 3:00:00 AM12/24/98
to
Frederic Martinelli <aer...@club-internet.fr> writes:

i'm coding C since quite some time now, but i never get reply to a
simple (?) question, in books nor over the Net : Can someone explain
me as deep as it's possible (even asm level related i.e. how the
compiler handle those cases) what are the differences between :

type *foo;
type* foo;
type * foo;

Nothing at all. Whitespace is insignificant here. In fact

type*foo;

will work just as well. Note also that the first form is IMHO the
least misleading, since

type *foo, bar;

is equivalent to

type *foo;
type bar;

not

type *foo;
type *bar;
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
do not ask me C questions via email; post them instead

Parker Shaw

unread,
Dec 25, 1998, 3:00:00 AM12/25/98
to
> type *foo;
> type* foo;
> type * foo;

They are all the same(at least to the compiler)

If you hope to declare more than one object at a time, first form is
better.
If you only declare one object at a time, second one is better.
I have no idea about the third one.

--
________________________________________________
Parker Shaw | Make it correct
psh...@cs.auckland.ac.nz | Make it elegant
________________________________________________

Lawrence Kirby

unread,
Dec 25, 1998, 3:00:00 AM12/25/98
to
In article <368367EF...@usa.net>
psh...@cs.auckland.ac.nz "Parker Shaw" writes:

>> type *foo;
>> type* foo;
>> type * foo;
>
>They are all the same(at least to the compiler)
>
>If you hope to declare more than one object at a time, first form is
>better.
>If you only declare one object at a time, second one is better.

The second form is misrepresenting the language syntax so isn't a good
idea under any circumstances. Some argued that which form to use
is a purely subjective thing, but this point at least is not subjective.

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------


Judson E. Knott

unread,
Dec 25, 1998, 3:00:00 AM12/25/98
to
On Fri, 25 Dec 1998, Lawrence Kirby wrote:
> >> type *foo;
> >> type* foo;
> >> type * foo;
>
> The second form is misrepresenting the language syntax so isn't a good
> idea under any circumstances. Some argued that which form to use
> is a purely subjective thing, but this point at least is not subjective.
>

Well, their is no difference, but as L. Kirby says it does misrep the
syntax, but they are all legal so it really a matter of preference. I like
the first way, but teachers I've had like to mix the second and third in
the code they gave us so you need to realize they are all the same, and be
prepared to see them all.

+--------------------+---------------------------------------+
| Judson E. Knott | http://www4.ncsu.edu/~jeknott/ |
| 222A Bragaw Hall | or http://knott.rh.ncsu.edu/~jeknott/ |
| NCSU Box 15298 | ICQ #: 17605659 |
| Raleigh, NC. 27607 | PHONE: (919) 512-9176 |
+--------------------+---------------------------------------+


Rich

unread,
Dec 26, 1998, 3:00:00 AM12/26/98
to
>> type *foo;
>> type* foo;
>> type * foo

<<snip>>

>The second form is misrepresenting the language syntax so isn't a good
>idea under any circumstances. Some argued that which form to use
>is a purely subjective thing, but this point at least is not subjective.

Maybe I'm slow - I don't see how int* 'misrepresents the language syntax' -
can you explain this? Incidentally, this question is covered as one of the
FAQs at Bjarne Stroustrup's homepage:

http://www.research.att.com/~bs/bs_faq.html#whitespace

Rich.

Jack Klein

unread,
Dec 26, 1998, 3:00:00 AM12/26/98
to

<Jack>

As Ben stated some, especially newcomers, might misread:

int* x, y;

to mean that both x and y are pointers to int, when in fact x is a pointer to
int and y is just an int.

It is misleading because even though you put the asterisk to the left next to
the type, it does not bind to the type. It binds to the variable name on the
right so:

int *x, y;

uses white space to emphasize that the "pointer to" only applies to x, not to
int and thereby to both x and y.

In C++ the "&" to indicate "reference to" has the same issues.

</Jack>


Rich

unread,
Dec 26, 1998, 3:00:00 AM12/26/98
to

>It is misleading because even though you put the asterisk to the left next
to
>the type, it does not bind to the type. It binds to the variable name on
the
>right so:
>
>int *x, y;
>
>uses white space to emphasize that the "pointer to" only applies to x, not
to
>int and thereby to both x and y.

Yes, I see what you mean. I personally find

int* x;

easier to read... I guess you have to take care with declarations no matter
what syntax is used. In any case, thanks for the clarification.

Cheers,

Rich.


Lawrence Kirby

unread,
Dec 26, 1998, 3:00:00 AM12/26/98
to
In article <9146410...@Chaos.es.co.nz> nos...@dev.null "Rich" writes:

>>> type *foo;
>>> type* foo;
>>> type * foo
>
><<snip>>
>
>>The second form is misrepresenting the language syntax so isn't a good
>>idea under any circumstances. Some argued that which form to use
>>is a purely subjective thing, but this point at least is not subjective.
>
>Maybe I'm slow - I don't see how int* 'misrepresents the language syntax' -
>can you explain this?

Syntactically all declarations are of the form

declaration-specifiers init-declarator-list(opt)

(where (opt) means optional). Here ``type'' is a declaration-specifier and
``*foo'' form an init-declarator-list. So ``type'' and ``*foo'' are
syntactic elements of the language. ``type*'' is meaningless as far as
the language syntax is concerned. The important thing to realise from this
is that the * is bound to the name on its right, not to the type on its
left so, for example:

int *p, i, *p2;

declares p and p2 to be pointers to int but i as just an int.

> Incidentally, this question is covered as one of the
>FAQs at Bjarne Stroustrup's homepage:
>
>http://www.research.att.com/~bs/bs_faq.html#whitespace

Yes, however I disgree that this is a C vs. C++ thing as he tries to suggest.
That theory quickly gets into trouble when you start to consider arrays,
functions and multiple declarators (which can't be ignored).

Greg Comeau

unread,
Dec 27, 1998, 3:00:00 AM12/27/98
to
In article <k8zi3y...@localhost.localdomain> Frederic Martinelli <aer...@club-internet.fr> writes:
>Hello,

>
> i'm coding C since quite some time now, but i never get reply to a
>simple (?) question, in books nor over the Net : Can someone explain
>me as deep as it's possible (even asm level related i.e. how the
>compiler handle those cases) what are the differences between :
>
>type *foo;
>type* foo;
>type * foo;

Assuming say type == int, the compiler sees them all the same.
The difference is just your spacing, and so a matter of style.
That is to say, in all your cases, foo is a pointer to a type.

- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C/C++ 4.2.38 -- New Release! We now do Windows too.
Email: com...@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
*** WEB: http://www.comeaucomputing.com ***

Greg Comeau

unread,
Dec 27, 1998, 3:00:00 AM12/27/98
to
In article <87ww3hz...@pfaffben.user.msu.edu> pfaf...@pilot.msu.edu writes:

>Frederic Martinelli <aer...@club-internet.fr> writes:
>
> i'm coding C since quite some time now, but i never get reply to a
> simple (?) question, in books nor over the Net : Can someone explain
> me as deep as it's possible (even asm level related i.e. how the
> compiler handle those cases) what are the differences between :
>
> type *foo;
> type* foo;
> type * foo;
>
>Nothing at all. Whitespace is insignificant here. In fact
>
> type*foo;
>
>will work just as well. Note also that the first form is IMHO the
>least misleading, since
>
> type *foo, bar;
>
>is equivalent to
>
> type *foo;
> type bar;

A general rule of thumb seems to be worthy of consideration here too. It is:
Consider avoiding declaring multiple entities in one declaration/definition.

Greg Comeau

unread,
Dec 27, 1998, 3:00:00 AM12/27/98
to
In article <368367EF...@usa.net> psh...@cs.auckland.ac.nz writes:
>> type *foo;
>> type* foo;
>> type * foo;
>
>They are all the same(at least to the compiler)
>
>If you hope to declare more than one object at a time, first form is
>better.
>If you only declare one object at a time, second one is better.
>I have no idea about the third one.

That such non/possibilities exist is enough reason to hope to
declare only one object at a time.

Greg Comeau

unread,
Dec 27, 1998, 3:00:00 AM12/27/98
to
> psh...@cs.auckland.ac.nz "Parker Shaw" writes:
>
>>> type *foo;
>>> type* foo;
>>> type * foo;
>>
>>They are all the same(at least to the compiler)
>>
>>If you hope to declare more than one object at a time, first form is
>>better.
>>If you only declare one object at a time, second one is better.
>
>The second form is misrepresenting the language syntax so isn't a good
>idea under any circumstances. Some argued that which form to use
>is a purely subjective thing, but this point at least is not subjective.

Seems to me that with this argument that all forms misrepresent the syntax.

Greg Comeau

unread,
Dec 27, 1998, 3:00:00 AM12/27/98
to
In article <3685644...@netnews.worldnet.att.net> jack...@att.net (Jack Klein) writes:
>On Sat, 26 Dec 1998 16:55:56 +1300, "Rich" <nos...@dev.null> wrote:
>> >> type *foo;
>> >> type* foo;
>> >> type * foo
>> <<snip>>

>>
>> >The second form is misrepresenting the language syntax so isn't a good
>> >idea under any circumstances. Some argued that which form to use
>> >is a purely subjective thing, but this point at least is not subjective.
>>
>> Maybe I'm slow - I don't see how int* 'misrepresents the language syntax' -
>> can you explain this? Incidentally, this question is covered as one of the

>> FAQs at Bjarne Stroustrup's homepage:
>>
>> http://www.research.att.com/~bs/bs_faq.html#whitespace
>>
>As Ben stated some, especially newcomers, might misread:
>
>int* x, y;
>
>to mean that both x and y are pointers to int, when in fact x is a pointer to
>int and y is just an int.
>
>It is misleading because even though you put the asterisk to the left next to
>the type, it does not bind to the type. It binds to the variable name on the
>right so:
>
>int *x, y;
>
>uses white space to emphasize that the "pointer to" only applies to x, not to
>int and thereby to both x and y.

The problem here is that some don't see this emphasis and so each can
misleading. If one wants clear emphasis, then declare only one
thing per declaration.

Tom Torfs

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
Greg Comeau wrote in a message to Jack Klein:

>int *x, y;

>uses white space to emphasize that the "pointer to" only applies to
>x, not to int and thereby to both x and y.

GC> The problem here is that some don't see this emphasis and so each
GC> can misleading. If one wants clear emphasis, then declare only
GC> one
GC> thing per declaration.

Well, it seems reasonable enough to expect programmers that have to
read your programs (including yourself) to know at least the basics of
C, such as this. So the emphasis isn't really the issue, but
consistency may be.

For example:

FILE *infile, *outfile;
int *count;

vs.

FILE* infile, *outfile;
int* count;

vs.

FILE *infile, *outfile;
int* count;

Of these only the first seems consistent to me. The second one is
inconsistent because the infile and outfile definitions look different
although they are of the same type. The third one is inconsistent
because the infile/outfile and count definitions place the * in a
different position.

What you propose seems to be:

FILE *infile;
FILE *outfile;
int *count;

or

FILE* infile;
FILE* outfile;
int* count;

The first is apparently equally good as the first one above, it just
adds some more lines of code (may be handy if you're paid by LOC but
otherwise seems rather pointless).

The second one suffers from the same potential inconsistency problem as
number 2 above, should another definition/declaration be added (e.g. by
someone else who doesn't put them all on separate lines).

greetings,
Tom
tomt...@village.uunet.be


John Bode

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
In order to mutate this thread...

In article <17536...@f516.n292.z2.fidonet.org>, tomt...@village.uunet.be
(Tom Torfs) wrote:

[snippity]

> What you propose seems to be:
>
> FILE *infile;
> FILE *outfile;
> int *count;
>
> or
>
> FILE* infile;
> FILE* outfile;
> int* count;
>
> The first is apparently equally good as the first one above, it just
> adds some more lines of code (may be handy if you're paid by LOC but
> otherwise seems rather pointless).
>

Just a quick comment about that comment -- speaking as both someone who
does new development and maintenance, I prefer the one declaration per line
style. It makes it easier for me to find individual elements (I can search
a vertical list faster than a horizontal one). And I most certainly
*don't* get paid by LOC ;-)

As this is a stylistic issue, feel free to disagree.

--
John Bode
one grumpy code monkey

A Programmer writes code that will run at the end of the day.
A Software Engineer writes code that will run ten years from now.

To email me directly, remove the 'nospam.' from my address.


0 new messages