pointer as parameter

Hi all.
I have a small problem concerning loosing the information of the pointer adress by going back from a subfunction to the main function.
I define a struct

struct pointer
{
int number
... // and many more but it doesn't matters
}

1
2
3
4
5
6
7
8
9
int main()
{
  pointer *ChoosenOpenFatherProblem;
  int status;    
  status = ChooseSubproblem(ChoosenOpenFatherProblem);
  cout << "adress of the pointer ChoosenOpenFatherProblem: " << ChoosenOpenFatherProblem << endl;  // LOGICAL ERROR APPEARS BECAUSE THE ADRESS IS NULL
...
}


In the subfunction ChooseSubproblem, he definitely gets an address and so a value such that the cout of the address inside the subfunction works:

1
2
3
4
5
6
int ChooseSubproblem(pointer *ChoosenOpenFatherProblem)
{
...
cout << "adress of the pointer ChoosenOpenFatherProblem: " << ChoosenOpenFatherProblem << endl;  // DEFINITELY HAS AN ADRESS WHICH IS NOT NULL
}


In the subfunction the pointer is well defined, shows to an obkect and I can work with him! Also the cout in the subfunction shows me an adress as it should be!!!
My problem appears in the main function after calling the subfunction. Then the return adress of the pointer defined in the main function is still NULL in the cout EVEN if the subfunction there is the right adress.

Could you pleas help me... By the way it is not a compiler error (compiles without any problems) but the problem appears while running the programm.

Thanks a lot
Last edited on
pointers work just like other variables. If you pass it by value (which you are), when you change it in the function, the calling function won't change.

Example:

1
2
3
4
5
6
7
8
9
10
11
void func(int v)
{
  v = 5;  // changes func's copy of v, does not change main's v
}

int main()
{
  int v = 0;
  func(v);
  cout << v;  // this prints 0
}



You're doing the same thing, just with a pointer:

1
2
3
4
5
6
7
8
9
10
11
void func(int* p)
{
  p = whatever_value_that_isnt_null;
}

int main()
{
  int* p = 0;
  func(p);
  // here, p is still null
}


If you want func to actually change 'p', you need to pass it by reference. Or you need to return it. Either one of these will work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//  pass by reference
void func(int*& p)
{
  p = whatever;
}

int main()
{
  int* p = 0;
  func(p);  // now this changes p, because p is passed by reference
}

///////////////////////////////////////
// return it
int* func()
{
  return whatever;
}

int main()
{
  int* p = func();
}
If you set the value of the pointer in the function, the value will not be returned to the main because you are passing by value. If you want to set the value in the function use pass by reference.
You could use a & or a ** (a pointer to a pointer) in the function definition.
Thanks a lot! This was definitely something I had not yet a lot of experience with and especially the first detailed answer helped me a lot. My problem is also solved.

So thank you very much (both)!
Topic archived. No new replies allowed.