type *var -- vs. -- type* var 
Author Message
 type *var -- vs. -- type* var

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    *



Wed, 17 Feb 1993 17:18:00 GMT  
 type *var -- vs. -- type* var

Quote:

>    type* var;
>rather than
>    type *var;

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

Quote:
>    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.


{uunet,harvard}!think!barmar



Wed, 17 Feb 1993 18:38:00 GMT  
 type *var -- vs. -- type* var
=>
=>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_|



Wed, 17 Feb 1993 19:47:00 GMT  
 type *var -- vs. -- type* var

Quote:

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




Wed, 17 Feb 1993 19:52:00 GMT  
 type *var -- vs. -- type* var
Regarding declarations of pointers x, y and z, and question of why
'int* x, y, z' doesn't do what's 'intuitive':

Quote:
>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?

--



Wed, 17 Feb 1993 20:18:00 GMT  
 type *var -- vs. -- type* var

Quote:

>Why is this allowed?

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


Wed, 17 Feb 1993 00:53:00 GMT  
 type *var -- vs. -- type* var

Quote:

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


Wed, 17 Feb 1993 01:11:00 GMT  
 type *var -- vs. -- type* var

Quote:

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

Right.

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

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

Quote:
>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)



Wed, 17 Feb 1993 04:09:00 GMT  
 type *var -- vs. -- type* var

Quote:

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


Wed, 17 Feb 1993 10:35:00 GMT  
 type *var -- vs. -- type* var

Quote:

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


{uunet,harvard}!think!barmar



Wed, 17 Feb 1993 17:10:00 GMT  
 type *var -- vs. -- type* var

Quote:


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

Quote:
>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.


Wed, 17 Feb 1993 19:41:00 GMT  
 type *var -- vs. -- type* var

Quote:

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

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



Wed, 17 Feb 1993 23:45:00 GMT  
 type *var -- vs. -- type* var


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



Wed, 17 Feb 1993 03:21:00 GMT  
 type *var -- vs. -- type* var

Quote:

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

--
--

  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.



Wed, 17 Feb 1993 02:25:00 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. Args: var number & var types

2. CONFLICT - Global Var of a .LIB Vs Global Var of a program which uses the LIB

3. extern var vs. static extern var

4. var type: (char (*)[256])

5. what does a (type **)var statement do?

6. Q on var types in STDMETHODs

7. Passing user defined type var between VB and VC

8. VC5.0 CDialog::OnOK() mangles type COleDateTime member var

9. Non-constant initializer for constant type var

10. var = !var;

11. Concatenate CString var + int var

12. How to change CString var into int var?

 

 
Powered by phpBB® Forum Software