pass by reference using ampersand in parameter list

I have one very quick question regarding the title topic. I understand the concept of pass by reference and pass by value. However im a little confused by one thing. Suppose i have the attached code..Now, i am passing an int to the function someFunc()..But someFunc is expecting "the address of an int" i.e int&, and not an int.. Why doesnt the compiler complain?

int a = 5;

someFunc(int& x){}
int a = 5;

someFunc(int& x){}

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:

Depending on your compiler settings, this may generate a warning, but not an error.

On many modern systems, integers and pointers are both 32-bit entities.  To pass an integer with a value of 5 to the function, the compiler generates the same instructions as if it were passing the address of the integer.

What you get is a function that is passed a 5, which it treats as an address.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
>> But someFunc is expecting "the address of an int"
int a = 5;
someFunc(int& x){ int y = x;}

Open in new window

In your C++ code, someFunc is not expecting "the address of an int" (i.e., a pointer to an int), but rather, someFunc is expecting a reference to an int. In the someFunc example, y is assigned to x; y now has the integer value 5.

Since your code includes references, then it is better not to include the C-zone, since C does not include the reference sematics.
oggiemcAuthor Commented:
hi phoffric,

Ok im a little confused.. i thought & was used to define "the address of a variable"?? i also thought that "address of" and "reference to" are one and the same..can you clear up my misinterpretaion please?
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

evilrixSenior Software Engineer (Avast)Commented:
>>  i thought & was used to define "the address of a variable"
The & operator has multiple meanings depending on context.


Open in new window

means take the address of x
int & x

Open in new window

means define a reference of type int called x


The same is true for the * operator


Open in new window

means dereference x (dereference the pointer to the type it points to)
int * x 

Open in new window

means define a pointer to int called x
oggiemcAuthor Commented:
hi evilrix,

i understood the different meanings asterisk could have and i always wondered whether ampersand could have multiple meanings too.. so i guess it does!! so if i understand you correctly:

int& x..... is basically a reference to the int value passed in to the function??
Happy New Year!

Suppose you have:
  int &x; // x is a reference to an int; but error
    - this reference needs initialization; for example:
  int int_var;
  int &x = int_var;  // Now, whenever you use x, you are referring to int_var.

In a function, e.g.,
void foo( int & y ) {
      int my_int;
      my_int = y;
y is a refererence to an int. It also needs an initialization to some actual int variable. This occurs when you call the foo function and pass it an int variable. For example:

foo( int_var );   // now, y in the foo function is actually the int_var variable.
Be careful - if you were to modify y in the above foo function, then int_var changes.

Take a look at these pass by reference examples:
evilrixSenior Software Engineer (Avast)Commented:
>> int& x..... is basically a reference to the int value passed in to the function??

To add, a reference value is similar to a pointer value -- both they contain some address.  The reference value is more restricted -- it must be the address of the existing memory space.  You cannot fill the reference variable by some placehodler with the meaning of NULL.  

The second difference is that the reference value is automatically dereferenced when used.  This way it looks as if you worked with the actual variable with a second name.  Syntactically, you cannot say from looking at the snippet of the code if you work with the original variable or with the reference to that variable (unless you can see the declaration).

From that point of view, passing by reference means passing the address of the variable that will be dereferenced automatically (unlike in the pointer case).  The reference variable stores the address.  Here the & syntax in the declaration of a reference variable makes sense.
oggiemcAuthor Commented:
ok thanks all..i think i finally inderstand this going to split the points between all, because everyone has contributed to my understanding on this..thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.