www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why * goes before type name?

reply GamblerZG <GamblerZG_member pathlink.com> writes:
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?
Jul 07 2005
parent reply "Regan Heath" <regan netwin.co.nz> writes:
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
Jul 07 2005
parent "Walter" <newshound digitalmars.com> writes:
"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.
Jul 08 2005