[comp.lang.c] type *var -- vs. -- type* var

SMITHJ@ohstpy.mps.ohio-state.edu (09/09/89)

I have been learning C++ from a book by Bjarne and he consistently declares
pointers with
	type* var;
rather than
	type *var;
At first I thought it was a C++ extension but when I tried it on my VAX/VMS
compiler it accepted the latter.  Why is this allowed?  It would be nice to
use type* to declare a series of pointers but this notation currently just
names the first var a pointer and the rest a regular variables.
i.e.
	int* x, y, z;
is equivalent to
	int *x, y, z;
rather than
	int *x, *y, *z;
as would be intuitive (to me anyway).

-- 
They have one big advantage over us: 
		*they* know where they're going.
Has your family tried 'em, Powdermilk?

/* Jeffery G. Smith, BS-RHIT (AKA Doc. Insomnia, WMHD-FM)       *
 *    The Ohio State University, Graduate Physics Program       *
 *        3193 Smith Lab, Columbus, OH 43210  (614) 292-5321    *
 *    smithj@ohstpy.mps.ohio-state.edu                          */

barmar@think.COM (Barry Margolin) (09/09/89)

In article <4201@ohstpy.mps.ohio-state.edu> SMITHJ@ohstpy.mps.ohio-state.edu writes:
>	type* var;
>rather than
>	type *var;

They are equivalent.  The "*" is a separate token, and whitespace
between tokens is not significant.

>	int* x, y, z;
>is equivalent to
>	int *x, y, z;
>rather than
>	int *x, *y, *z;
>as would be intuitive (to me anyway).

That's because "*" binds more tightly than ",".  If you want to force
a particular grouping, use parentheses:

	(int *) x, y, z

Barry Margolin
Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar

gdtltr@vax1.acs.udel.EDU (Gary D Duzan) (09/09/89)

In article <29048@news.Think.COM> barmar@think.COM (Barry Margolin) writes:
=>
=>That's because "*" binds more tightly than ",".  If you want to force
=>a particular grouping, use parentheses:
=>
=>	(int *) x, y, z
=>
Warning: Stupid Question Follows.

   How does the compiler distinguish between the above and a cast and two
comma operators?
					Gary Duzan
					Time  Lord
				    Third Regeneration




-- 
      _o_                                                            _o_
    [|o o|]        "Two hearts are better than one." -- Yes        [|o o|]
     |_O_|      "Don't listen to me; I never do." -- Doctor Who     |_O_|

karl@haddock.ima.isc.com (Karl Heuer) (09/09/89)

In article <4201@ohstpy.mps.ohio-state.edu> SMITHJ@ohstpy.mps.ohio-state.edu writes:
>I have been learning C++ from a book by Bjarne and he consistently declares
>pointers with `type* var;' rather than `type *var;'

They're syntactically identical; whitespace isn't significant between tokens.
It's just Bjarne's personal convention.  I also dislike it, for the reasons
you noted, and I wish he'd used the more conventional style.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

lewie@pur-ee.UUCP (Jeff Lewis) (09/09/89)

Regarding declarations of pointers x, y and z, and question of why
'int* x, y, z' doesn't do what's 'intuitive':

>That's because "*" binds more tightly than ",".  If you want to force
>a particular grouping, use parentheses:
>
>	(int *) x, y, z
>

'(int *) x' is a cast, not a declaration.  Compiling the above yields
errors about x, y and z being undeclared.  Because there's no explicit
separator between declarations and statements, you'll never be able
to declare pointers as such.  Isn't C syntax fun?

--
Jeff Lewis (lewie@ee.ecn.purdue.edu, pur-ee!lewie)

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/09/89)

In article <4201@ohstpy.mps.ohio-state.edu> SMITHJ@ohstpy.mps.ohio-state.edu writes:
>Why is this allowed?

The only correct answer is "Because Dennis decided to do it that way."

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/09/89)

In article <12813@pur-ee.UUCP> lewie@ecn-ee.UUCP writes:
>Because there's no explicit separator between declarations and statements,
>you'll never be able to declare pointers as such.  Isn't C syntax fun?

I don't know what you're talking about; do you?
Anyone who programs in C frequently declares pointers as such.

chris@mimsy.UUCP (Chris Torek) (09/09/89)

In article <29048@news.Think.COM> barmar@think.COM (Barry Margolin) writes:
>They are equivalent.  The "*" is a separate token, and whitespace
>between tokens is not significant.

Right.

>>	int* x, y, z;
>>is equivalent to
>>	int *x, y, z;

(which is why I, for one, will not use the former layout).

>That's because "*" binds more tightly than ",".  If you want to force
>a particular grouping, use parentheses:
>
>	(int *) x, y, z

Nice idea, but you had better go read a C BNF.  Parentheses are not
permitted there.  You can, however, use typedef:

	typedef int *pointer_to_int;

	pointer_to_int x, y, z;
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

ok@cs.mu.oz.au (Richard O'Keefe) (09/09/89)

In article <29048@news.Think.COM>, barmar@think.COM (Barry Margolin) writes:
> That's because "*" binds more tightly than ",".  If you want to force
> a particular grouping, use parentheses:
> 	(int *) x, y, z

I like it.  I like it a lot.  But
	cat >zabbo.c <<end_of_file.
	(int *) x, y, z;
	main() { x = 0, y = 0, z = 0; exit(0); }
	end_of_file.
	gcc -ansi -pedantic zabbo.c
run on a Sun-3/50 under SunOS 4.0 using gcc version 1.35 starts out
	zabbo.c:1: parse error before `int'
Are you _sure_ this is legal?

barmar@think.COM (Barry Margolin) (09/10/89)

In article <12813@pur-ee.UUCP> lewie@ecn-ee.UUCP writes:
>>	(int *) x, y, z
>'(int *) x' is a cast, not a declaration.

Thanks to everyone who has pointed out my error.  Sorry for the
confusion.

Barry Margolin
Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar

karzes@mfci.UUCP (Tom Karzes) (09/12/89)

In article <10992@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes:
>In article <12813@pur-ee.UUCP> lewie@ecn-ee.UUCP writes:
>>Because there's no explicit separator between declarations and statements,
>>you'll never be able to declare pointers as such.  Isn't C syntax fun?
>
>I don't know what you're talking about; do you?

I don't think you were paying close enough attention to this discussion.
Yes, he knows what he's talking about.  His point was that if the following
is seen in a routine (amidst declarations):

    (int *) x, y, z;

then it will be parsed as a comma expression and the (int *) would be
parsed as a cast.  It certainly won't serve as a declaration of three
pointers to ints, which is what was desired.  Furthermore, because it
is already legal syntax for an expression, and because the parser would
have no way of knowing when the declaration section of a block has ended
until it sees a statement (this is what he meant about no explicit
separator), he is pointing out that C could not be compatibly extended
to accept this syntax for declarations.  Hence, you'll never be able
to declare pointers as such (using the above syntax).

>Anyone who programs in C frequently declares pointers as such.

This statement is a nonsequitur.  C programmers do frequently declare
pointers to ints, but they certainly don't use the above syntax to do
so, which is what you unwittingly claimed.

tainter@cbnewsd.ATT.COM (johnathan.tainter) (09/12/89)

In article <12813@pur-ee.UUCP> lewie@ecn-ee.UUCP writes:
>Regarding declarations of pointers x, y and z, and question of why
>'int* x, y, z' doesn't do what's 'intuitive':
>>	(int *) x, y, z
>'(int *) x' is a cast, not a declaration.  Compiling the above yields
>errors about x, y and z being undeclared.  Because there's no explicit
>separator between declarations and statements, you'll never be able
>to declare pointers as such.  Isn't C syntax fun?

This is a place where typedef is not equivalent to a macro.

typedef int *Pointer_to_int;
Pointer_to_int x, y, z;

does exactly what was desired with only one side effect, it defines
a typedef.  Don't try and use a screwdriver as a chisel, especially when
you already have the chisel.

>Jeff Lewis (lewie@ee.ecn.purdue.edu, pur-ee!lewie)

--johnathan.a.tainter--
   att!ihlpb!tainter
   

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/12/89)

In article <1014@m3.mfci.UUCP> karzes@mfci.UUCP (Tom Karzes) writes:
-In article <10992@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes:
->In article <12813@pur-ee.UUCP> lewie@ecn-ee.UUCP writes:
->>Because there's no explicit separator between declarations and statements,
->>you'll never be able to declare pointers as such.  Isn't C syntax fun?
->Anyone who programs in C frequently declares pointers as such.
-This statement is a nonsequitur.  C programmers do frequently declare
-pointers to ints, but they certainly don't use the above syntax to do
-so, which is what you unwittingly claimed.

I got a note from lewie; it turns out that the misunderstanding lies in
the usage of the phrase "as such".  He meant in the cast-like format.
I would have said "like that" instead of "as such"..

diamond@csl.sony.co.jp (Norman Diamond) (09/13/89)

In article <29048@news.Think.COM> barmar@think.COM (Barry Margolin) writes:

>That's because "*" binds more tightly than ",".  If you want to force
>a particular grouping, use parentheses:
>	(int *) x, y, z

And use which compiler?

--
-- 
Norman Diamond, Sony Corporation (diamond@ws.sony.junet)
  The above opinions are inherited by your machine's init process (pid 1),
  after being disowned and orphaned.  However, if you see this at Waterloo or
  Anterior, then their administrators must have approved of these opinions.