Functions with Pointers as Parameters

May 7, 2016 at 6:21pm
Hi!

My issue with this piece of good is in the function void doit().

The function needs two memory addresses: &foo (address of pointer foo) and goo (which is a pointer).

Why does it work when two integers, instead of addresses, are passed to it?

Here is the raw code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void DoIt(int &foo, int goo);

int main() {
  int *foo, *goo;
  foo = new int;
  *foo = 1;
  goo = new int;
  *goo = 3;
  *foo = *goo + 3;
  foo = goo;
  *goo = 5;
  *foo = *goo + *foo;
  DoIt(*foo, *goo);
  cout << (*foo) << endl;
}

void DoIt(int &foo, int goo) {
  foo = goo + 3;
  goo = foo + 4;
  foo = goo + 3;
  goo = foo;
} 


And here is the code with my comments and cout statements to figure out what is going on:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

void doit (int &foo, int goo) {
  cout << "foo and goo are " << foo << ' ' << goo << endl;
  foo = goo + 3; // Add 5 to *foo. The result is 13.
  cout << "foo and goo are " << foo << ' ' << goo << endl;
  goo = foo + 4; // goo is 13 plus 4. The result is 17.
  cout << "foo and goo are " << foo << ' ' << goo << endl;
  foo = goo + 3; // foo is 12 plus 3. The result is 15.
  cout << "foo and goo are " << foo << ' ' << goo << endl;
  goo = foo; // goo is 15.
}

int main() {
  int *foo, *goo;
  foo = new int; // Allocate some space for an integer pointed to by foo.
  *foo = 1; // The value pointed to by foo is 1.
  cout << "*foo is " << *foo << endl;
  goo = new int; // Allocate some space for an integer pointed to by goo.
  *goo = 3; // The value pointed to by goo is 3.
  cout << "*goo is " << *goo << endl;
  *foo = *goo + 3; // The value pointed to by foo is 6.
  cout << "*foo is " << *foo << endl;
  foo = goo; // goo's address is now stored in foo. foo now points to *goo.
  cout << "foo and goo are " << foo << ' ' << goo << endl;
  *goo = 5; // goo points to 5. So does foo.
  cout << "*goo is " << *goo << endl;
  *foo = *goo + *foo; // foo's new value is 10.
  cout << "*foo is " << *foo << endl;
  cout << "*goo is " << *goo << endl;
  cout << "Now begins the function." << endl;
  doit (*foo, *goo); // *foo and *goo are 10.
  cout << (*foo) << endl; // 20, which is correct.
  return 0;
}
Last edited on May 7, 2016 at 6:21pm
May 7, 2016 at 7:03pm
The function needs two memory addresses: &foo (address of pointer foo) and goo (which is a pointer).


No it doesn't. Let's look at the function:
void doit (int &foo, int goo) {

The first parameter, foo, is an int reference.
The second parameter, goo, is an int.

Neither parameter is a pointer. This function doesn't take addresses (pointers) as parameters. That's why it works when you pass it int values. Because that's what these parameters are.
May 7, 2016 at 7:10pm
Thank you! The first parameter is not a pointer. What's happening is that foo's value is being passed onto the function by reference.
May 7, 2016 at 7:11pm
The second parameter also is not a pointer.
Topic archived. No new replies allowed.