Ampersand as a variable SUFFIX?

What is it called? What does it mean? It's either an object variable or some kind of reference to a class. I'm not sure which.

I am reading a tutorial about overloading operators and it just popped up inexplicably in some of the examples. I must have missed it in the text and can't find it.

& means address-of. It is used to pass the address of a variable, instead of the value of a variable, to a function.

Passing the address means that the function can alter the value of the variable. Passing the value means that whatever the function does to the value is lost when the function ends.

"suffix": POST-fix. Ending. Comes after.

thisVar&

Not

&thisVar

Still the same?

After a second look, I guess it's a type.

const Var& param

Type, Right?

References are designed to be used as parameters (arguments) to functions.

int x = 6;

void setup() 
{
  Serial.begin(9600);
  squareNum(x);
  Serial.println(x);
}

void loop() 
{
}

void squareNum(int& i)
{
  i*=i;
}
[quote author=BulldogLowell link=msg=2679073 date=1458838444]
References are designed to be used as parameters (arguments) to functions.

void squareNum(int& i)
{
  i*=i;
}

[/quote]

So, why use it? couldn't you have just as correctly used:

void squareNum(int i)

After a second look, I guess it's a type.

const Var& param

Type, Right?

Yes. The space doesn't matter, so I prefer

const type &param

or

const type *ptr;

so that it is more obvious that the variable/parameter is a reference/pointer.

It also makes it clearer when doing something like this

int *ptr, notAPointer;

that only one variable in the list is a pointer (though anyone that does that deserves to be shot).

Writing that as

int* ptr, notAPointer;

makes it look like the type of both variables will be pointer to int, while that is not the case.

So, why use it?  couldn't you have just as correctly used:

void squareNum(int i)

Try it. Print the value of i in the function and in setup(). See what happens with and without the &.

And, then re-read the first reply to understand why the result is not the same.

BulldogLowell:
References are designed to be used as parameters (arguments) to functions.

int x = 6;

void setup()
{
 Serial.begin(9600);
 squareNum(x);
 Serial.println(x);
}

void loop()
{
}

void squareNum(int& i)
{
 i*=i;
}

Is this C++ specific? The version I know is

void squareNum(int *i)
{
  *i = *i * *i;
}

and call it like

int x = 6;
squareNum(&x);

Is this C++ specific?

No. Passing variables by reference is something that works in C, too.

Why confuse things with pointers? The pass-by-reference code looks cleaner.

PaulS:
Yes. The space doesn't matter, so I prefer

const type &param

or

const type *ptr;

so that it is more obvious that the variable/parameter is a reference/pointer.

Surely, you realize those are not exactly the same, and cannot be used inter-changeable, as those two arguments would be used differently within the functions?

A reference argument is a much "cleaner" way of using a variable by reference than a pointer. Pointers exist only due to the c legacy behind the c++ language definition. If c++ were being designed today, minus it's c legacy, I'm quit sure pointers would not exist at all as a language element, while reference arguments most certainly would.

Granted, reference arguments are simply "syntactic candy" on top of the same under-lying operation, but they are nonetheless different in use, and the whole point of reference arguments is to allow use of pointers without the typically ugly c pointer syntax. Once an argument is passed to a function as a reference, the variable name alone can be used to read or write that variable, without having to explicitly de-reference it. It is simpler for the programmer, and cleaner. The fact that it is a reference is equally clear, whether using * or & in the argument declaration.

Regards,
Ray L.

PaulS:
Yes. The space doesn't matter, so I prefer

const type &param

or

const type *ptr;

so that it is more obvious that the variable/parameter is a reference/pointer.

It also makes it clearer when doing something like this

int *ptr, notAPointer;

that only one variable in the list is a pointer (though anyone that does that deserves to be shot).

Writing that as

int* ptr, notAPointer;

makes it look like the type of both variables will be pointer to int, while that is not the case.

Okay, I see it. The space was throwing me. This is one of the things I don't like about C... Too many ways to say exactly the same thing. I also saw it used as an argument in a class constructor without the variable. So it was just

myClass::myClass (varR&, int i, int j)

I see what they were doing now.

PaulS:

So, why use it?  couldn't you have just as correctly used:

void squareNum(int i)



Try it. Print the value of i in the function and in setup(). See what happens with and without the &.

And, then re-read the first reply to understand why the result is not the same.

PaulS:

So, why use it?  couldn't you have just as correctly used:

void squareNum(int i)



Try it. Print the value of i in the function and in setup(). See what happens with and without the &.

And, then re-read the first reply to understand why the result is not the same.

To me it looks like the example below is trying to square an address.

void squareNum(int& i)
{
  i*=i;
}

Am I wrong?

To me it looks like the example below is trying to square an address.

Code: [Select]

void squareNum(int& i)
{
i*=i;
}

Am I wrong?

Well, since the very idea of squaring a pointer is meaningless, I'm going to say yes, you're wrong.

AWOL:
Well, since the very idea of squaring a pointer is meaningless, I'm going to say yes, you're wrong.

AWOL:
Well, since the very idea of squaring a pointer is meaningless, I'm going to say yes, you're wrong.

Did you not see the part where I said this?? Now you're just confusing me.

To me it looks like the example below is trying to square an address.

chipwitch:
Did you not see the part where I said this?? Now you're just confusing me.

To me it looks like the example below is trying to square an address.

the pass by reference method is an important part of the C++ lexicon. Just like any other language, once you commit to understand it, it becomes invisible.

Questioning why something looks oddly spelled in another language makes as much sense as your argument above.

chipwitch:
Okay, I see it. The space was throwing me. This is one of the things I don't like about C... Too many ways to say exactly the same thing.

In c and c++, white space is meaningless, unless inside a quoted string, and a few other places. 99% of the time, you can have white space, or you can leave it out, and it will make no difference in the meaning of the code.

That's what you can do things like this, which is perfectly valid c code:

                #include<stdio.h>/*IOCCC2014            2014    2014    IOCCC2014IOCCC2014IOCCC201*/
                char*s="\"nsu{AntynCnuq}Bnu{            sEot    ln>b    )+c^g+@`+]_osk{;j@bkg&c<'^o\
                r'Q]                    bh'l    vQ^k                    g&c:                    %n|\
                N]_o                    ptj9    lwg+                    )d:b                    kg$\
                c8#^    #g+)d8`a%g+)    d8`_                g&;bh'oq    Q^)g    +&kcNlyMc+)d    8`a\
                `g+@    u)|d8ak=bl}(    Q^og                (O{MK6lM    L(rR    pOpM866OsRlm    N(q\
                Q]##    OsR#M_(lQoOa    N9$m    vOwwRor~        }(cN    mkM:    q(Q]%_(uU]}_    {8b\
                %mRh    #S^`#mOaaD%/    RI4$    4SNH$%N4        RlMG    /2MJ    24O3NF(tQ?1l    N*N\
                +Q]l    mq9l8b$^#h$#    .d,d    xv#mSOPm    R8`/        lM;b    h&^/lM:k8b%&    Q^-\
                h%c9    .#,/&N$McPc%    -d8,    $c8``7:b    %^h%        :79b    $^%$7ON8r%Qr    h$Q\
                On%q                    N%O~        M$qN$OMp    RmPQ    O%rQ                    $rQ\
                MkQN                    77O#        dj#Nkd$7    O%d8    ``(r                    RmM\
                :(luN](%mRMmO%lNRmO_loa8%%O<    b(lQ    h'lQ    O^ln    ;b'%N&O^6sN#M9slltwzh&TmS

Regards,
Ray L.
                %R#ON$oNS&pRM7O
[/code]

Regards,
Ray L.%S#ON$oNS&p    SMd9    (t    pkO9    $:nk8b#^%}kx#O%lMtpk#OQOP:#\                                                 mR8`}(qQ        O<b\                                                 '7M{N^kh        'Q]\                     7sN'    M8q;k'N_7vN'nSM8    'nR;    $%M'    TamS&7O#d#7O    &d8`z9b$    ^h$\                     rQ]6    yN$_$pQaM8m{px$    TmSt    6O#N    sM$nRmUd#7Os    d77sNOsd    8`j\                 =nk;        |8k9    b1g(    Q^)$c:%]    k*Q]g$9%_h*]+wnS        _+sTaa:&%{R\                 M<%{        S{O_    &<#w    kP8#a;'(    0d:`184>b)^15)OM        :23h)QN<'/M\                     =b(^'h(Q]0h#    c_kah%0d9`b'/O(Q^(/SlM90%M(/            RM;&    $c&kckQ,    -$c\                     .&kccMcOP§_DISCOURSE_HOISTED_CODE_0_§d    9g(=6:69j=b5g(Q^3(2lNUO<b4            'Q^s    ;b&^%%pQ    M8k
                        $7O#cQ]0(2(Q]'2O    3'Q]'3a    N_1'    5OMaMch&T$mR#nRMPmSk    U$7
                        O#d8_a%%mS]l
%mR    mS]$]h$9    j_la    $6N]g$9j_laaaN:g'<    ``7\                 oM:6%N9b%g$Q        ^6%N8b%g    #Q^l            peoqemtemw#jQ7#QO$jQ    O7$Q    Ok#\                 $7OMQ]k_#$7O        ckQaOrON    epne            lueluelpeoqempepneu    ekf    _a
                                                lf))    +,**,)-)/),0).0(6/2+667,(&    $##
                                                #;=@    D0;#include<stdio.h>@intYH[    9%\                 .],*s,*c,d,t;;'main(-9n,ch            ar*v    []){        for(s=c=    H+3XI;
                (
++s=Q[d++]););for(;n>1&&(*            ++s=    v[1]        [tgANs=H    ;d=*c++
                %93,                    d-9;    ){in    tYv=s,g        []={    n+v,        v-n,n

                ,#^_                    ,,<v    ,n?v    /n:0,#%        ,,v    >>n,        v==n}ay
                >t=0    ;d<4&&d>=2*!    !n&&    (c-=d/32_KK3+c++,t||v!=98+d);)        t+=v++/6-16
                ?0:v    /2%3-1cq
d-1    4;t>    0_t$<3_z(105<c_X'=t21aq@-106;n        =d>76?s--,g
                [d-7    7]:d>55?H_1)    21]=    n,
_@8(        9?*++s_4&12>d_Cf3]+=21-d*2\                 :d<3    4?t:_Zadat57    <d?p    utchar(n        ),v:6<a](g+99]=a{d2#:xa6,n
                ;}re    turnY
.",*p,    b,d[        9338    ],*q,x,*r=d;            int main(){;for(
                p=q=    5000+0+d;s;    s++)        if(    s>32)*p++=*s            -89?*s:32;for(p=
                1152                    +q;(    b=*p++);){for(d[17]=            10;x    =*p++,b<
                92&&                    34<b    --;r++=x)if(x==99)            for(    ;*q;x=34
                )*r++=*q++;for(p-=b<92;b-->4        *23;r++)    *r=r[36-    x];}puts    (d);
                return(0);}/IOCCC2014IOCCC2        014IOCCC    2014IOCC    C2014IOC    CC/
[/code]

Regards,
Ray L.

Surely, you realize those are not exactly the same, and cannot be used inter-changeable, as those two arguments would be used differently within the functions?

What I was referring to was keeping the symbol with the variable, not the type. Yes, I fully understand that reference variables and pointers are not interchangeable.

Pointers exist only due to the c legacy behind the c++ language definition.

Excuse me, but that is nonsense. Only Microsoft thinks that hiding the fact that pointers are heavily involved in any code that does much of anything is a good idea.

To me it looks like the example below is trying to square an address.

No. If white space were added to that statement, you'd see that the operator is *= which is a compound operator. Documented on the reference page for you perusal.

Questioning why something looks oddly spelled in another language makes as much sense as your argument above.

You mean like people that talk about coloured LEDs? WTF? Can't they spell?

In c and c++, white space is meaningless, unless inside a quoted string, and a few other places.

To the compiler, maybe. Nottopeopletryingtoreadit.

PaulS... I'm still confused. Some of the responses sound like they're contradicting one another.

void squareNum(int& i)
{
  i*=i;
}
  1. in the previous code, the value of "i" is a memory address (int &i).
  2. if i is an address, then

i*=i; is a compound operator, which is the same as i^2. Since i is an address, that code is squaring an address, which is a useless, though possible. Why is i not an address?

chipwitch:
PaulS... I'm still confused. Some of the responses sound like they're contradicting one another.

void squareNum(int& i)

{
 i*=i;
}




1) in the previous code, the value of "i" is a memory address (int &i).
2) if i is an address, then

i*=i; is a compound operator, which is the same as i^2. Since i is an address, that code is squaring an address, which is a useless, though possible. Why is i not an address?

1.) False. i as an object of type "reference to int"
http://www.embedded.com/electronics-blogs/programming-pointers/4023307/References-vs-Pointers explains it well

In the code if you have void squareNum(int i) instead of void squareNum(int &i), you will see that 6 is printed and not 36.