0

My understanding of the following function that it asks for the address of an integer variable.

void MyFunction(int & myParameter)

but in my code for example I have:

int myVar;
myFunction(&myVar)

I get a compiler error saying: no known conversion from 'int*' to 'const int&'.

when I pass 'myVar' without '&' the compiler is happy. I am bit confused, what is happening here. Also what is the difference between the pointer sign '*' and the address sign '&' in this context? when should I use either? Is there a difference between c and c++ for the usage of '&'? Thanks in advance.

MIbrah
  • 1,007
  • 1
  • 9
  • 17
  • 1
    You could read this question : http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome – Thomas Ayoub Feb 12 '16 at 15:00
  • 3
    I think what is confusing you is that `&` can have two very different meanings. It can be the address of operator, like in `&myVar`, this gives you a pointer to `myVar`. On the other hand, in a type declaration `&` stands for reference like in `MyFunction(int& parameter)`. To really understand it you will have to do some reading on pointers and references. – 463035818_is_not_an_ai Feb 12 '16 at 15:06
  • 4
    To add a third, it's also stands in for bitwise and. – Bathsheba Feb 12 '16 at 15:17

4 Answers4

9

Just call myFunction (myVar). The compiler then will pass your variable by reference, which means, that if you change that variable in a function, this variable will be changed outside of a function as well.

As for the difference between * and &. If you have a variable of some type (let it be int) and you want to get its pointer, you can apply get the address using the operator &:

int i;
int* pointer_to_i = &i;

If your function takes int*, you then need to pass either &i or pointer_to_i (they are obviously the same thing). If your function takes int or int&, then you should pass just i.

A function taking its argument by value (like void f(int);) deals with the copy of its argument. All the changes this function makes to this argument are not visible outside. If a function takes its argument by either pointer or reference, all the changes made to the argument are visible outside.

When you get a pointer as an argument, you need to:

  1. Check that it is not NULL in C or nullptr in C++
  2. Dereference it. That is, you should write *i = 0 to put zero into the memory pointed to by i.

Other than that, passing by pointer (like void f(int*){…}) or by reference (like void f(int&){…}) effectively differs only in syntax and has the same meaning.

Pointers can point nowhere and can be made to point to another value. References on the contrary always point to something and always point to the same memory location.

That's in brief. But you should really read some decent C++ book, there are many tricky cases with references or pointers.

And, finally, a mnemonic rule.

int i; //< that's a plain number
int* pi = &i; //< that's a pointer to int
int (*pi_2) = &i; //< the same pointer to int
int j = *pi_2; //< here *pi_2 is again an int just as above

That said, this asterisk indicating a pointer can be prepended to a variable, thus 'stripping' one indirection level from its type. The explanation is not quite correct formally, but sometimes handy to understand what happens.

gluk47
  • 1,812
  • 20
  • 31
5

Your understanding is not correct. It's a reference.

You call it like this:

myFunction(myVar);

Inside the function, myParameter will be the same as myVar, unlike an ordinary function call (without the &) in which myParameter would be a copy of the value of myVar.

Reference argument do not exist in C, there you'd have to use a pointer argument (int *myParameter).

So, the & in the argument list does not mean "pointer", it means reference.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

if you want to pass the variable value to a function, just call it like this:

myFunction(myVar);

If you want to pass the variable's adress to the function it should be this way:

myFunction(&myVar);

BUT, in order to make the second call work, your function must accept as parametre a pointer of the SAME type of your variable: for example if your variable is an int:

int myVar;

then the function's prototype should be:

myFunction(int* var);

the same applies for other types, I hope it helped.

Yassir Khaldi
  • 1,452
  • 1
  • 17
  • 30
-3

Do:

void MyFunction(int *myParameter)

And call it like this:

int myVar;
myFunction(&myVar)
odin19
  • 147
  • 1
  • 8
  • Changing the function signature isn't an answer to the question on how to actually call a function with a specific signature. – Simon Kraemer Feb 12 '16 at 15:11