Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

basic c++ question, difference between a and a&

c++ variable variabletype

  • Please log in to reply
1 reply to this topic

#1 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 295 posts

Posted 10 October 2017 - 01:46 PM

Hello,

 

i am a new convert to c++, why, its because C++ is the base of so many thing, plus, i want a challenge, not that i dont get it from other langauges like C# or python, and those are good langauges in my opinion, but c++, i always been fond of it, always looked at it as the next stepping stone to a challenge, so, simple stupid noob question, what is the difference between declaring and using variable a and variable a&?

 

can you give an example as to why and when to use each or any?

 

thanks in advanced


  • 0
Its only funny till someone gets hurt.... THEN ITS HILARIOUS :)

#2 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1310 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 25 October 2017 - 02:53 PM

Hello, and welcome to the world of C++! 

 

The ampersand (&) in C++ does one of two things to variables: designates variables as references, or gives the memory address of the variable. Do you know memory addresses already? 

 

First of all, let me start with the latter case, where & tells C++ to use an address rather than the object itself. The variable with the address is called a pointer. Compare this code: 

void myFunc (int number) { 
    printf ("Number: %d", number); 
} 

int main (int argc, char * argv []) { 
    int x = 4; 
    myFunc (x); 
    return 0; 
} 

To this code: 

void myFunc (int * number) { 
    printf ("Number: %d", *number); 
} 

int main (int argc, char * argv []) { 
    int x = 4; 
    myFunc (&x); 
    return 0; 
} 

In the latter case, I changed the function parameter type from "int" to "int *" (a memory address of an 'int' rather than the 'int' itself). The "&" is now required when calling myFunc () because C++ needs an address (& tells it to use the address) rather than a value. 

 

However, pointers are not recommended, so you should avoid them when possible (see this SO answer). Luckily, C++ has references. Under the hood, references are just pointers, but programming them is usually better than programming pointers. Changing the code above to use references: 

void myFunc (int & number) { 
    printf ("Number: %d", number); 
} 

int main (int argc, char * argv []) { 
    int x = 4; 
    myFunc (x); 
    return 0; 
} 

Notice that only one part changed: an "&" for the one argument of myFunc (). So the parameter type is "int &" rather than "int". The myFunc () gets a reference to "x" rather than a value, so myFunc () could change our "x" if it wanted to. Like so: 

void changeMe (int & number) { 
    number = 10; 
} 

int main (int argc, char * argv []) { 
    int x = 1; 
    printf ("%d; ", x); 
    changeMe (x); 
    printf ("%d; ", x); 
    return 0; 
} 

The changeMe () function has full access to the integer, as if it had a pointer to it, so the second printf () statement should print "10;" 

 

You can use references yourself too, not just as function parameters. Take this: 

int main (int argc, char * argv []) { 
    int x = 10; 
    int & y = x; 
    y++; 
    printf ("%d", x); 
    return 0; 
} 

The printf () statement should print "11" because y was a reference to x, and we incremented it. 

 

There is also a binary operation with "&", which is not for declaring variables. Therefore, I won't go into it in my answer, but you can read about it in the Wikipedia article

 

Summary: 

  • It can be a way to tell C++ to use a variable's memory address rather than its value. 
  • It tells C++ to grab a reference (under the hood: pointer) to the object rather than its value. 

  • 0
Regards,
RR