-2

In this example:

#include <iostream>
using namespace std;

void foo(int& x)
{
   x = x%2 + 10;
}

int* goo(int& y)
{
    y = y/100 + y%10;

    return &y;
}

void main()
{
    int temp = 1234;
    int *p = goo(temp);
    cout << temp << endl;
    foo(*p);
    cout << temp << endl;
}

There is the goo function that gets an int address, But you can see that main has an int temp that you send to this function. The function should get an address but instead it gets int how is it possible?

I ran the program and it does work and I do not understand why.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
liran
  • 1
  • 2
    All questions on stackoverflow.com must include all pertinent information in the question itself, as plain text. Links to external web sites, that can stop working at any time, rendering the question meaningless, are not acceptable. – Sam Varshavchik Mar 25 '17 at 10:53
  • Can you post your code here instead of a diagram ? – Mohammad Tayyab Mar 25 '17 at 10:54
  • 1
    `goo` does not get an int address. – melpomene Mar 25 '17 at 10:58
  • No, the program most definitely does not "work". `goo()` is declared as returning a pointer to an int, but it fails to return anything; and the return value gets dereferenced. This is undefined behavior. – Sam Varshavchik Mar 25 '17 at 11:05
  • @Sam Varshavchik I edited question, somehow return statement got thrown out of code I added instead of screenshot OP provided, a merge bug? – Swift - Friday Pie Mar 25 '17 at 11:17
  • Careful with the `foo` `goo`. Cut it wrong and it's very toxic. – user4581301 Mar 25 '17 at 11:23
  • @user4581301 ..and everything gets `foo` `bar`ed – Swift - Friday Pie Mar 25 '17 at 11:25
  • I'm voting to close this question as off-topic because the question appears to be based on a misunderstanding. `goo` takes an `int` by reference, not a pointer to `int`. – user4581301 Mar 25 '17 at 11:31
  • @user4581301 all questions are based on misunderstanding, i.e. on lack of knowledge. The argument doesn't make much of sense. This reference mistake is common among those who started to learn C++ from learning C by K&R. Maybe there was such question already but formulated differently – Swift - Friday Pie Mar 25 '17 at 11:46
  • @Swift questions need to demonstrate a minimum understanding of the language and syntax otherwise the asker will not be able to interpret the answers. In this case the asker is better off [consulting a good introductory text](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) than asking questions based on basic C++ syntax. – user4581301 Mar 25 '17 at 17:48

1 Answers1

0

That's not an "address of int". Loosely defined, address of int would be pointer, that's what goo is returning. It get passed a reference to storage of int type, or speaking in C terms, an int parameter is passed by reference (in C that would mean that a pointer is passed). references are close to pointers but aren't subject to pointer math and have same syntax as a variable.

Function goo() returns a pointer to the parameter passed by reference, essentially it returns pointer to temp in this case. Those are frequent declarators used in combination to type identifier:

  • type& var - a reference to type
  • type* var - a pointer to type
  • type&& var - an rvalue reference to type

Variables of reference type cannot be unitialized and they can be initialized only by lvalue. A reference is evaluated to an lvalue, evaluated to the same storage as the lvalue that was used for initialization.

int b = 222;
int *p = new int(111);

int& refb = b;  // evaluated to the storage of b;
int& refp = *p; // evaluated to the storage in dynamic memory, pointed by p;
int& illegal = 2; // ERROR: not a lvalue;

Declarations of rvalue reference is rarely used aside of formal parameter list of overloaded operators, move semantics and template building.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42