Newbie Q: pointer and reference syntax

U

USCode

I've noticed that in various books and online sources that sometimes I see
the & addressof operator and the * dereference operator at either the
beginning of the variable name or at the end of the variable name. Is this
just syntactic or personal style preference or is there a difference in how
they're being used?

before:
*my_ptr
&my_ref

after:
my_ptr*
my_ref&

Thanks!
 
D

David Harmon

On Thu, 17 Jun 2004 00:23:18 GMT in comp.lang.c++, "USCode"
I've noticed that in various books and online sources that sometimes I see
the & addressof operator and the * dereference operator at either the
beginning of the variable name or at the end of the variable name. Is this
just syntactic or personal style preference or is there a difference in how
they're being used?

before:
*my_ptr
&my_ref

after:
my_ptr*
my_ref&

Totally different. If you want to know more, you would have to give an
example in context.
 
J

John Harrison

USCode said:
I've noticed that in various books and online sources that sometimes I see
the & addressof operator and the * dereference operator at either the
beginning of the variable name or at the end of the variable name. Is this
just syntactic or personal style preference or is there a difference in how
they're being used?

before:
*my_ptr
&my_ref
Legal


after:
my_ptr*
my_ref&

Illegal

Obviously you didn't understand the example you saw. I think maybe you are
mixing up use of addressof and dereference with declaration of pointers and
references. But why not quote a specific example? That should sort out your
confusion pretty quickly.

john
 
J

JKop

USCode posted:
I've noticed that in various books and online sources that sometimes I
see the & addressof operator and the * dereference operator at either
the beginning of the variable name or at the end of the variable name.
Is this just syntactic or personal style preference or is there a
difference in how they're being used?

before:
*my_ptr
&my_ref

after:
my_ptr*
my_ref&

Thanks!


void TakesPointer(const int* const);

void TakesReference(const int&);

void TakesPlain(const int);


int main(void)
{
int a = 78;

TakesPointer(&a);

TakesReference(a);


int* pA;

pA = &a;

TakesPointer(pA);

TakesReference(*pA);


int& b = a;

TakesPointer(&b);
TakesReference(b);

TakesPlain(a);
TakesPlain(b);
TakesPlain(*pA);

//Now, the multiplication operator:

a = b* 5;
a = b *5;
a = b*5;

//And the bitwise AND operator:

a = b& 5;
a = b &5;
a = b & 5;

}


Hope that helps.

-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top