Connecting Tech Pros Worldwide Forums | Help | Site Map

Indirection operator placement

Marty
Guest
 
Posts: n/a
#1: Aug 21 '06
I am wondering what is the difference in the placement between the 2
uses of the indirection operator? Or is there a difference?

AcDbBlockTable *pBlockTable = NULL;

AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();

tolgaceylanus@yahoo.com
Guest
 
Posts: n/a
#2: Aug 21 '06

re: Indirection operator placement



There's no difference.

Tolga Ceylan

Frederick Gotham
Guest
 
Posts: n/a
#3: Aug 21 '06

re: Indirection operator placement


Marty posted:
Quote:
I am wondering what is the difference in the placement between the 2
uses of the indirection operator? Or is there a difference?
>
AcDbBlockTable *pBlockTable = NULL;
>
AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();

A C++ compiler ignores white space, so it sees all of the following
identically:

int*p=0;
int *p=0;
int* p = 0;
int * p = 0 ;

I think it's intuitive, however, to write it as follows:

int *p = 0;

, because it conveys that the asterisk is a part of the object's name --
which is how things actually work:

int *p, *q, r[5], (*s)[5], *t[5], Func(), *Func2(), (*Func3())[5];

p is a pointer.
q is a pointer.
r is an array of 5.
s is a pointer to an array of 5.
t is an array of 5 pointers.
Func is a function which returns an int.
Func2 is a function which returns a pointer to an int.
Func3 is a function which returns a pointer to an array of 5.

--

Frederick Gotham
Howard
Guest
 
Posts: n/a
#4: Aug 21 '06

re: Indirection operator placement



"Marty" <mcoonrod@gmail.comwrote in message
news:1156176433.765400.289740@i42g2000cwa.googlegr oups.com...
Quote:
>I am wondering what is the difference in the placement between the 2
uses of the indirection operator? Or is there a difference?
>
AcDbBlockTable *pBlockTable = NULL;
>
AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();
>
I assume you're talking about the "*" symbols in the code above? That's not
an operator. It specifies that the object being declared is a pointer to
the type specified on the left of the * symbol. The spacing is irrelevant.
The following are all the same:

int* pInt;
int *pInt;
int * pInt;
int * pInt;

All those line declare a variable named pInt whose type is "pointer-to-int".

Some people prefer to put the * immediately after the type, others prefer to
put it immediately before the identifier, and still others prefer to put
spaces on both sides.

The compiler doesn't care one way or the other. :-)

-Howard


Marcus Kwok
Guest
 
Posts: n/a
#5: Aug 21 '06

re: Indirection operator placement


Marty <mcoonrod@gmail.comwrote:
Quote:
I am wondering what is the difference in the placement between the 2
uses of the indirection operator? Or is there a difference?
>
AcDbBlockTable *pBlockTable = NULL;
>
AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();
This is answered on Stroustrup's FAQ:
http://www.research.att.com/~bs/bs_faq2.html#whitespace

In short, they are the same. It's merely a matter of style which form
you use. However, as noted on the referenced page, the confusion comes
when someone tries to declare multiple pointers with a single
declaration:

int* p1; // pointer to int
int *p2; // also pointer to int

int* p3, p4; // p3 is a pointer to int, p4 is a regular int
int* p5, *p6; // both p5 and p6 are pointers to int

Since I prefer one declaration per line, this doesn't affect me, so I
use the (int* p;) form instead of the (int *p;) form, for the reason
given on the page: (int* p;) emphasizes the type (p is a pointer to
int), versus (int *p;) which emphasizes the syntax (*p is an int).

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Marty
Guest
 
Posts: n/a
#6: Aug 21 '06

re: Indirection operator placement



Thanks all!
I am new to C++ (if you can't tell) but you confirmed what I thought
was the answer.
When I started looking through the example project, they used it as I
had shown.
It started to get me to double guess myself.

Closed Thread


Similar C / C++ bytes