im trying to learn pointer n the &(no idea the specific name for it); so my question is..
im passing the memory address of P to changePTR, why p is not taken the address of o(lets assume o is dynamic allocated), or even i want to change p to NULL. but p in main is still keeping the address of k?

can anyone refer sum good tutorial for learing pointer and memoery location stuffs.... they are so confusing >.<

#include <iostream>
using namespace std;

void changePTR(int* p){
	int z = 100;
	int* o = &z;
        p = o;
        // p = null     // this doesnt work also..??
	}

int main(){
	int k = 1;
	int* p = &k;
	int* q = p;
	changePTR(p);
	
	cout<<p<<" and "<<&k<<" and "<<&q<<endl;
}

Recommended Answers

All 5 Replies

void changePTR(int* p){
	int z = 100;
	int* o = &z;
        p = o;
        // p = null     // this doesnt work also..??
	}

I know you said assume that 'o' is dynamically-allocated, but it's not, and given this example code that's not a reasonable assumption. There are significant logical differences between dynamic allocation and this code. This code as written is a big no-no. You're returning a reference to a local variable, which you should not do. When you do this, the pointer points at a variable that gets destroyed when the function ends; which means it's pointing at invalid data.

// p = null     // this doesnt work also..??

This doesn't work because C++ is case-sensitive. The proper syntax is p = NULL; , NOT p = null . "NULL" and "null" are different identifiers, and "null" does not exist.

The operator '&' is called the "reference operator". It is used several different ways. In your particular situation, it is used to get the reference (memory address) of the variable immediately following it for storage in a pointer.

Pointers are very consistent in their behavior. Your problem can be simplified by removing a level of indirection:

void changePTR(int p)
{
    p = 100;
}

int main()
{
    int p = 1;

    cout<< p <<'\n';
    changePTR(p);
    cout<< p <<'\n';
}

Since p is passed by value (ie. changePTR is working with a copy of the value), any changes to p inside changePTR affect the copy and not the original object. To modify the original object you need to get a reference to it with another level of indirection:

void changePTR(int *p)
{
    *p = 100;
}

int main()
{
    int p = 1;

    cout<< p <<'\n';
    changePTR(&p);
    cout<< p <<'\n';
}

Now let's add a level of indirection and make p itself a pointer in main. The same issue exists because pointers are passed by value just like ints, and the solution is the same. Add a level of indirection to get a reference to the original object:

void changePTR(int **p)
{
    *p = 0;
}

int main()
{
    int i = 1;
    int *p = &i;

    cout<< p <<'\n';
    changePTR(&p);
    cout<< p <<'\n';
}
commented: still confuse on my materials.. but he helped me out =D cool guy +2

@Narue
u saved my life... they(pointer n &) still confuse me.., just wonder if u have any good reference guides to read =D..
definitely rep you, ty so much ~~

@Fbody
opps.. acutally i was trying to mean NULL, tis is wat i trying to mean~

#include <iostream>
using namespace std;
// im trying to make p(at main) to point at the new z at changePTR

    void changePTR(int* ptr){
    int* z = new int(999);
    ptr = z;
    // p = NULL  
    cout<<"Memory loc of Z: "<<z<<" the value of Z: "<<*z<<endl;
    z = NULL;
    }
     
    int main(){
    int k = 1;
    int* p = &k;
    int* q = p;
    changePTR(p);
     
    cout<<"Memory loc of P: "<<p<<" the value of P: "<<*p<<endl;
    }

**** very appreciate for both of your help *****

just wonder if u have any good reference guides to read =D..

I usually recommend this one, but beware that I'm biased since I wrote it. ;)

this is cool =D..
i will take my time to read thought everything
and ty again =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.