0

I have this situation.

 case 1
    void printInt(int & i) {}

    int main () {
       int i=1;
       printInt(&i);

    }

the printInt is expecting a reference, so therefore, inside the main, I call the printInt function and supplied with the reference i. Is this correct.

then I can also do

 case 2
     int main () {
       int i=1;
        printInt(i);    // i is a lvalue, and printInt function is expecting a lvalue

}

so, are case 1 and case 2 seems like conflicting?

lilzz
  • 5,243
  • 14
  • 59
  • 87
  • 1
    The first case is not correct at all. This sample is not valid C++. – Marcin Łoś Aug 31 '13 at 14:42
  • In "case 1", the two `&` have _two different meanings_: `int & i` (in `void printInt(int & i)`) declares `i` as an `int&` (reference to int), but `&i` (in `printInt(&i);`) means "address of `i`" and gives a pointer to `i` (of type `int*`, pointer to int). – gx_ Aug 31 '13 at 14:46
  • @ gx_ when you print out int&, reference to an int, what do you get? – lilzz Aug 31 '13 at 14:51
  • In C you use pointers to pass values by reference, it's not needed in C++ as it ha proper references. – Some programmer dude Aug 31 '13 at 14:53
  • @lilzz A reference is just an alias. Given `int i=1; int& r = i;` then anything (printing, modification) done to `r` (the "alias") is actually done to `i` (the "original"). It seems that you should read a good C++ introductory book/tutorial. For books, see http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – gx_ Aug 31 '13 at 15:04

3 Answers3

2

The first case doesn't even compile because you are passing int* but the compiler expects reference to a int type.

printInt(&i);

In the above statement, you are passing the address of an integer. So, at the receiving end it should be a pointer.

Edit:

I think you are confused between pointers and references. Think of reference as an alias to a variable. An analogy to some extent would be like a short cut for an application placed on the desktop. When you click the short cut, you are actually running the executable installed in the Applications directory. So, short cut is more like an alias for running the executable of the application.

Can short cut placed for one application be used to open different application ? No. For a similar reason, references can not be reseated.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • so what's a reference? it's not an address? then what is it? – lilzz Aug 31 '13 at 14:49
  • 1
    In the implementation, a reference is a pointer, all right. On the language level, however, a reference is treated as a value, hiding the fact that actually an address is taken. As such, to call your function, you would have to write `printInt(i)`, because the compiler takes the address implicitely. Confused? That's normal. References are among the most evil inventions ever! – cmaster - reinstate monica Aug 31 '13 at 14:52
1

Case 1 is actually incorrect, it is expecting a reference-able integer, however you are supplying it with pointer-to-integer.

Erbureth
  • 3,378
  • 22
  • 39
0

The second case is correct. When you pass by reference a value int x to function foo(int &y) you write:

foo(x);

This is as if you said:

int &y = x;

In the first case you call

foo(&x);

which is as if you said:

int &y = &x;

This is not how a reference is defined.

cpp
  • 3,743
  • 3
  • 24
  • 38