Pass a pointer by reference C only

I thought if I passed a pointer by reference I could change it. Am I doing this correctly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
     func1();
}

void func1()
{
    int *x;
    func2(&x);
    //I want to use x here after I changed it in func2
}

void func2(int **x)
{
     //do stuff with x
}

This is what I've got except I'm using structs instead of ints.  Is that how the argument should look for func2?  Also when I work with x in func2 do I need any speacial syntax like (*x) or something, I'm a little confused on that.  Thanks
 
Yes, that's correct. Did you actually try it?

By the way, a pointer is only a pointer -- it doesn't point to anything until you tell it to.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int z = -7;
void func2(int* *x)
{
    *x = &z;
}

void func1()
{
    int  y = 42;
    int* x = &y;
    printf( "x = %d\n", *x );
    func2( &x );
    printf( "x = %d\n", *x );
}

int main()
{
    func1();
    return 0;
}

Hope this helps.
Is that how the argument should look for func2?

Yes. If you are passing the address of pointer, then a pointer to pointer should be declared to hold the address of pointer. So this is correct.


Also when I work with x in func2 do I need any speacial syntax like (*x) or something

dereferencing x i.e. (*x) will give you value of what is stored inside x of func2. And **x will give you the value what is stored at the address inside x.

I think you should read a little bit more about pointer concepts.
I am not sure what manipulation you would be doing on x (of func1) inside func2, but make sure that you are not assigning address of any variable which is local to func2.

Hope this helps !

Yeah I tried it and its doesn't seem to be changing it in the calling funtion.
Also when I try to to access it in func2 for example I have to do it like this
(*x)->data; //I'm using structs,

if I don't put the paranthesis around it the intellisense doesn't come up. What does that mean?

make sure that you are not assigning address of any variable which is local to func2.



Oh, yeah that is exactly what I'm doing. I guess thats why its not working.
(*x)->data; //I'm using structs,

-> is syntax for de-referencing a pointer, when it is holding the address of a struct or class, to access a data member. For ex.
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
struct myStruct { 

int i;
int j; 
} ; 

int main() { 

myStruct *ptr = (myStruct *) malloc (sizeof(myStruct));   
ptr->i = 10 ; // accessing data members by dereferencing pointer 
ptr->j = 12;   

cout<<"Old value of i is :"<<ptr->i<<endl; 
cout<<"Old value of j is :"<<ptr->j<<endl; 

myStruct **doublePtr = &ptr; 
(*doublePtr)->i = 20; 
(*doublePtr)->j = 12; 

cout<<"New value of i is :"<<ptr->i<<endl; 
cout<<"New value of j is :"<<ptr->j<<endl; 

free(ptr); 

return 0; 
} 


You need (*doublePtr)-> because, doublePtr is a double pointer. So by (*doublePtr) syntax you are saying that, access the value which is stored in doublePtr (which is address of ptr)

Oh, yeah that is exactly what I'm doing. I guess thats why its not working.

If you are assigning the local variables value to x in func2 and trying to access x in func1 hoping for a change, It Won't Work. Because once the function call is finished local variable is out of scope and is no longer valid. Notice, in the example posted by Duoas, he is modifying the global variable, which is accessible to both functions and will be valid till the end of the program.

Hope this helps !

Edit: changed delete to free
Last edited on
Topic archived. No new replies allowed.