Tripple pointer. Passing pointers by reference to function.

If f1 and f2 are two user defined functions.
1
2
3
4
5
6
7
8
9
10
main(){
int *p;
f1(&p)
}
f1(**p){
f2(&p);//If I've to pass the pointer by reference again in another function, will I've to do something like this?
}
f2(***p){
**p = NULL;
}
Last edited on
There's no need to create a new pointer if your goal is to pass a pointer by reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstddef>
void f2(int*& p)
{
    p = NULL;
}
void f1(int*& p) 
{
    f2(p);
}
int main()
{
    int* p;
    f1(p);
}
Last edited on
In http://codepad.org/r9ud4Qsm same purpose is being solved but my question is: why in the definition of f1 (in my codepad.org code) p's address has not been passed and still it's passed by reference?
In order to pass a pointer by reference we must have to pass its address in the arguments of a function but in your case, I can't understand how it's passed by reference (though I'm sure that it's passed by reference).
In order to pass a pointer by reference we must have to pass its address

No, that's not what pass-by-reference means. You're describing creating a new pointer and passing the new pointer by value (which is a common C programming technique, but the codepad.org link implies you're talking about C++)
Topic archived. No new replies allowed.