0

I am new to pointer to class.I write simple code to show my question.
Why can p->num set value in f2? Pointer p isn't like n ? Just a local variable?
Aren't both n1 and p2 's scope only in f1 and f2 separately ? thanks
What is different between them!

class Node{
public:
   int num;
   Node* next;
};

void f1(Node n1){
   n1.num = 50;
}
void f2(Node*p2){
   p2->num= 100;
}
int main(){     
   Node n;
   f1(n);
   cout<<n.num<<endl;//output 0

   Node*p;
   f2(p);
   cout<<p->num<<endl;//output 100
   return 0;    
}
4
  • 1
    Node*p; f2(p); is undefined behavior as-is. Your p doesn't point anywhere. Also, use whitespace. Seriously. Code w/o whitespace is hard to read. As to why can a value be modified through a pointer: because that's how memory access works. You grab an address, dereference it, write to the memory at that address. Commented Nov 1, 2013 at 17:59
  • Because you're invoking undefined behavior. Anything can happen (I only wish that anything included my house somehow painting itself). The passed variable p is indeterminate. It has no stated value. Using it for evaluation in any way is undefined behavior. Commented Nov 1, 2013 at 18:00
  • Note that p doesn't actually point to anything, as you never allocated a Node for it to point to. Similarly, you never initialized num in n. Commented Nov 1, 2013 at 18:00
  • You can change class members via pointer. That's the point of introducing pointers in the first place (well, one of the points anyway). Commented Nov 1, 2013 at 18:04

3 Answers 3

2

Passing a pointer to a function is like passing by reference. I fact, that's basically how references work under the hood.

Also, you have undefined behavior in your code, for both cases. When you declare a local variable without assigning to it at the same time, it's value is indeterminate, and using that value for anything is undefined. Yes, even accessing n.num is undefined behavior, that you get the value 0 for that is just luck. And using a pointer like you do may even cause the program to crash.

Sign up to request clarification or add additional context in comments.

2 Comments

You think someone who doesn't understand pointers understands references?
* how references are emulated in C. (since C has no references.)
1

If this code is really printing 100 for you, you're extremely lucky (or unlucky, based on the point of view). There's a problem that p is not pointing to anything, so when you write through it, you write into random memory. You should initialise it, perhaps like this:

int main(){     
   Node n;
   f1(n);
   cout<<n.num<<endl;//output 0

   Node *p = &n;  //now `p` will point to the Node object `n`
   f2(p);
   cout<<p->num<<endl;//output 100
   return 0;    
}

This could also help answer your primary question: a pointer points to an existing object. Which means that while p2 is indeed local in f2(), it's a copy of the pointer p from main(). And they both point to the same object (object n in my modification), which is why f2() can modify an "external" object through its local copy of the pointer.

Comments

1
f1(n);

n is passed by copy. Therefore, it's value doesn't change in main().

f2(p);

p is passed by copy. However, p is a pointer and you are changing the value to where it points. Therefore, the value to what it points can be updated and seen in main().

Don't forget to allocate the memory to where p points.

Node n;
Node *p;
p = &n;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.