Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

address of operator context question

4 views
Skip to first unread message

nvangogh

unread,
Apr 3, 2013, 6:11:11 PM4/3/13
to
I am getting mixed up with the & operator.
It is used before an identifier to show a reference. So that:

struct somedata{ // ;};
somedata inst;
somedata &aref = inst; is a type somedata that is a lvalue reference
that will be bound to an object of the same type.

So if i see somedata& - with the operator after the type, what does this
denote?


Ramon F. Herrera

unread,
Apr 3, 2013, 6:16:11 PM4/3/13
to
Maybe I misunderstood, but are you asking about the difference
between:

somedata &aref = inst;

and

somedata& aref = inst;

If you are, the difference is none. Spacing is irrelevant in most
programming languages.

-Ramon

Ramon F. Herrera

unread,
Apr 3, 2013, 6:36:04 PM4/3/13
to
On Apr 3, 5:11 pm, nvangogh <nvang...@invalid.net> wrote:
Normally, I am on the question-asking side, but here I go...

Back in the C days (where I come from, being relatively newbie to C++)
the & operator worked on the item in its right side. Its effect is to
take the address of a variable.

Notice this:

int array[23];
struct {
double d;
} record;

memset(array, 0, sizeof(array));
memset(&record, 0, sizeof(record));

-Ramon

Keith Thompson

unread,
Apr 3, 2013, 6:38:32 PM4/3/13
to
The unary `&` operator is the address-of operator. If `foo` is of type
`blah`, then `&foo` is of type `blah*`. That's not what you're asking
about. (There's also a binary `&` bitwise-and operator.)

In a declaration, `&` denotes a *reference* type. Reference types
existing in C++, but not in C.

I'm sure that <http://www.parashift.com/c++-faq/references.html>
explains references better than I could

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson

unread,
Apr 3, 2013, 6:40:24 PM4/3/13
to
"Ramon F. Herrera" <ra...@conexus.net> writes:
> On Apr 3, 5:11 pm, nvangogh <nvang...@invalid.net> wrote:
>> I am getting mixed up with the & operator.
>> It is used before an identifier to show a reference. So that:
>>
>> struct somedata{ // ;};
>> somedata inst;
>> somedata &aref =  inst;  is a type somedata that is a lvalue reference
>> that will be bound to an object of the same type.
>>
>> So if i see somedata& - with the operator after the type, what does this
>> denote?
>
> Normally, I am on the question-asking side, but here I go...
>
> Back in the C days (where I come from, being relatively newbie to C++)
> the & operator worked on the item in its right side. Its effect is to
> take the address of a variable.

That's still exactly what it means in both C and C++.

nvangogh is asking about references.

I should have mentioned in my other followup that the `&` character,
when used to denote a reference, is not an operator.

Ramon F. Herrera

unread,
Apr 3, 2013, 6:47:23 PM4/3/13
to
On Apr 3, 5:40 pm, Keith Thompson <ks...@mib.org> wrote:
> "Ramon F. Herrera" <ra...@conexus.net> writes:
>
>
>
>
>
>
>
>
>
> > On Apr 3, 5:11 pm, nvangogh <nvang...@invalid.net> wrote:
> >> I am getting mixed up with the & operator.
> >> It is used before an identifier to show a reference. So that:
>
> >> struct somedata{ // ;};
> >> somedata inst;
> >> somedata &aref =  inst;  is a type somedata that is a lvalue reference
> >> that will be bound to an object of the same type.
>
> >> So if i see somedata& - with the operator after the type, what does this
> >> denote?
>
> > Normally, I am on the question-asking side, but here I go...
>
> > Back in the C days (where I come from, being relatively newbie to C++)
> > the & operator worked on the item in its right side. Its effect is to
> > take the address of a variable.
>
> That's still exactly what it means in both C and C++.
>

> nvangogh is asking about references.

That was my guess, was typing a follow-up answer, but said to myself:
"I am sure a real C++ guru will have a better explanation for that
one".

-Ramon

Ramon F. Herrera

unread,
Apr 3, 2013, 8:41:26 PM4/3/13
to

Consider the program below. That is the way things were done in C,
before C++ introduced references.

Notice all those pesky, cumbersome asterisks all over the place, plus
the fact that the caller (main) has to agree with the called function
to pass the address of the variable 'a'.

With a reference (1 character in the whole code!) we can make that
program much nicer and readable.

-Ramon

-------------------------------

#include <iostream>
using namespace std;

void
incr(int* number)
{
*number = *number + 321;
}

int
main()
{
int a = 123;
incr(&a);
cout << "a: " << a << endl;
}

Ramon F. Herrera

unread,
Apr 3, 2013, 8:54:09 PM4/3/13
to

This is the rewritten version. Should you want to change from
reference coding to non-reference, all you need to do is remove the
ampersand sign. The calling statement in main() remains syntactically
unmodified.

-Ramon

--------------------------

#include <iostream>
using namespace std;


void
incr(int& number)
{
number = number + 321;
}


int
main()
{
int a = 123;
incr(a); // this line does not care how a will be passed.
cout << "a: " << a << endl;
}

nvangogh

unread,
Apr 4, 2013, 10:34:29 AM4/4/13
to
On 03/04/13 23:16, Ramon F. Herrera wrote:
> On Apr 3, 5:11 pm, nvangogh<nvang...@invalid.net> wrote:
>> I am getting mixed up with the& operator.
>> It is used before an identifier to show a reference. So that:
>>
>> struct somedata{ // ;};
>> somedata inst;
>> somedata&aref = inst; is a type somedata that is a lvalue reference
>> that will be bound to an object of the same type.
>>
>
> > So if i see somedata& - with the operator after the type,
> > what does this denote?
>
> Maybe I misunderstood, but are you asking about the difference
> between:
>
> somedata&aref = inst;
>
> and
>
> somedata& aref = inst;
>
> If you are, the difference is none. Spacing is irrelevant in most
> programming languages.
>
> -Ramon
>
But some people write eg: string func1(value&) - This was what caused
the original confusion. A space makes the meaning clearer such as (value &).

nvangogh

unread,
Apr 4, 2013, 10:42:14 AM4/4/13
to
On 03/04/13 23:38, Keith Thompson wrote:
> nvangogh<nvan...@invalid.net> writes:
>> I am getting mixed up with the& operator.
>> It is used before an identifier to show a reference. So that:
>>
>> struct somedata{ // ;};
>> somedata inst;
>> somedata&aref = inst; is a type somedata that is a lvalue reference
>> that will be bound to an object of the same type.
>>
>> So if i see somedata& - with the operator after the type, what does this
>> denote?
>
> The unary `&` operator is the address-of operator. If `foo` is of type
> `blah`, then `&foo` is of type `blah*`. That's not what you're asking
> about. (There's also a binary `&` bitwise-and operator.)
>
> In a declaration, `&` denotes a *reference* type. Reference types
> existing in C++, but not in C.
>
> I'm sure that<http://www.parashift.com/c++-faq/references.html>
> explains references better than I could
>
An interesting fact is that references were introduced into C++
primarily to support operator overloading (Stroustrup 'Design &
evolution of C++ p85)

It's not a topic i have studied yet though. But i definately needed to
review some of what i read on references. References are now used a lot
for passing arguments to functions and getting return values - but that
was not the primary purpose as these operations could already be done by
pointers.

nvangogh

unread,
Apr 4, 2013, 10:46:29 AM4/4/13
to
Thanks Ramon - you can and probably should use pointers to pass
arguments to functions as you are a c programmer and used to doing this.
I understand now that references were not introduced in C++ especially
for this purpose but for operator overloading.

Ramon F. Herrera

unread,
Apr 4, 2013, 11:21:47 AM4/4/13
to
On Apr 4, 9:34 am, nvangogh <nvang...@invalid.net> wrote:

> But some people write eg: string func1(value&) -
> This was what caused the original confusion.
> A space makes the meaning clearer such as (value &).

Yes and no. :-)

Proper use of spacing (or lack thereof) makes the meaning MUCH
clearer. I have a standard that works quite well for me. When I am
given code written by others, I add/remove spacing so it conforms to
my practice.

The ampersand is a great example.

(1) When it is a prefix operator, I type it immediate to the left of
the "operated" variable (no spacing).

memset(&some_struct, 0, sizeof(some_struct));

(2) When it is a reference (postfix) I place it to the right, again
with no spacing:

SomeClass::my_function(SomeType& varname) {
}

(3) Finally when the ampersand denotes an infix operator (either one
'&' bitwise or '&&' boolean), I always use spacing on both sides:

bool isDone = condition && othercondition;

-Ramon

Ramon F. Herrera

unread,
Apr 4, 2013, 12:02:54 PM4/4/13
to
On Apr 4, 9:46 am, nvangogh <nvang...@invalid.net> wrote:
> On 04/04/13 01:54, Ramon F. Herrera wrote:
>
>
>
>
>
>
>
>
>
> > This is the rewritten version. Should you want to change from
> > reference coding to non-reference, all you need to do is remove the
> > ampersand sign. The calling statement in main() remains syntactically
> > unmodified.
>
> > -Ramon
>
> > --------------------------
>
> > #include<iostream>
> > using namespace std;
>
> > void
> > incr(int&  number)
> > {
> >      number = number + 321;
> > }
>
> > int
> > main()
> > {
> >      int a = 123;
> >      incr(a);     // this line does not care how a will be passed.
> >      cout<<  "a: "<<  a<<  endl;
> > }
>

> Thanks Ramon - you can and probably should use pointers
> to pass arguments to functions as you are a c programmer
> and used to doing this.

I _was_ a C programmer and hate using pointers. In fact, I criticize
coders that can't seem to make the change to C++ and keep on using
this type of construct:

some->whatever

when they should use:

some.whatever

-Ramon

Ramon F. Herrera

unread,
Apr 4, 2013, 12:15:42 PM4/4/13
to
On Apr 4, 9:42 am, nvangogh <nvang...@invalid.net> wrote:
> References are now used a lot for passing arguments
> to functions and getting return values

Allow me to make a small correction to your wording. The term "return
value" is used in cases like this:

int
incr(int number)
{
int sum = number + 321;
return sum;
}

Notice that only ONE value is returned so you can never say "return
values". What you mentioned is most accurately termed "variables being
modified":

void
SomeClass::my_func(Type1&, Type2&, Type3&, Type4&)

IOW, in the case below, the "return value" is 'sum', not 'number'.

-Ramon

int
incr(int& number)
{
int sum = number + 321;
number = 0;
return sum;
}

0 new messages