Consider this piece of code:
int *a, *b;
a = foo();
if (a)
b = a;
a = bar();
The problem is, when a updates by calling bar(), b also updates. However I want to make a backup by using b = a. What is the problem then?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
(The value at address a is assigned to the value at address b.)
What you are doing right now is making a and b point to the same place in memory. Then, if the value in a or b is updated, they pointers both point to the new value.
By the way, unless
bar()returns a pointer, you probably want*a = foo()and*a = bar().