Thread overview
Why * goes before type name?
Jul 08, 2005
GamblerZG
Jul 08, 2005
Regan Heath
Jul 08, 2005
Walter
July 08, 2005
I alway wondered why C and C++ have such syntax, and now D implements pointers in exactly the same way. Why? Pointer is the actual type of variable, just like int. Shouldn't it be "* int varName;"? Or even better "ptr int varName;". Could someone explain the rationale behind that asterisk thing?


July 08, 2005
On Fri, 8 Jul 2005 02:18:00 +0000 (UTC), GamblerZG <GamblerZG_member@pathlink.com> wrote:

> I alway wondered why C and C++ have such syntax, and now D implements pointers in exactly the same way. Why?

Not quite. D is left associative rather than right associative (I think I got that round the right way?).

So, when you write:
  int * p , s;

in D you've written:
  int* p;
  int* s;

as opposed to C/C++ where you've written:
  int* p;
  int  s;

In other words the * is associated with the typename, not the variable name in D. The opposite of C/C++.

As such you should get into the habit of writing the * next to the typename:
  int* p;

not the variable name:
  int *p;

as is done in C/C++.

(someone correct me if I have this all wrong)

> Pointer is the actual type of variable, just like
> int. Shouldn't it be "* int varName;"? Or even better "ptr int varName;". Could
> someone explain the rationale behind that asterisk thing?

I prefer to think of it as "integer pointer p" AKA "int* p" rather than "pointer to integer p" AKA "*int p".

Though now that I look at it, the "*int p" syntax does make it clear the * associates with the "int" rather than the "p". I wonder if this new syntax has parsing problems? if not...

Regan
July 08, 2005
"Regan Heath" <regan@netwin.co.nz> wrote in message news:opstkrevzv23k2f5@nrage.netwin.co.nz...
> (someone correct me if I have this all wrong)

Yes, you've got it right.