in c++ primer
"

void reset(int *ip)
     {
         *ip = 0; 
           ip = 0;   
     }

After a call to reset, the argument is unchanged but the object to which the argument points will be 0:"


I understand that the argument is unchanged, but why the object to which the argument points will be 0? since all you are working on is a copy of the argument pointer, not the argument itself, i think the object to which the argument points should also be unchanged.

Thanks

When you pass information into a function you do "make a copy of" the data. In that sense, you are correct.

However, this is where you are incorrect:
When you have an argument passed as a reference, or are passing in a pointer, the information passed/copied into the function is a memory address, not actual data. In this case, the memory address is either the address of the argument itself or another variable that exists in the calling code. When you dereference the pointer, (with '*') as in line 3 of your code sample, you are accessing the data contained at the memory address (pointed to by the pointer), not the "value" of the pointer itself. As a result, the function is able to modify the value of the argument in both the function's scope and the original scope.

In line 4 of your sample, you are modifying the actual memory address stored in the pointer, which makes it no longer valid. This is a change in local scope that is not reflected in the scope of the calling code.

Pointers can be confusing, it's understandable. Did you read the link I gave you in your other thread? Here it is 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.