Hi,
i am having this problem with c structure pointers. this is snippet from a larger program in which i use temp* to assign values to different other pointers but whenever i update temp* it automatically updates the value of all other assigned pointers as well.

pointer b points to pointer temp and after assigning this whenever i change temp value it automatically changes the b value as well.

can anyone please help me on how i can perform this assigning with value instead of memory reference.

Thanks.

#include <iostream>
#include <list>

using namespace std;

struct node {
	string data;
};

int main(){
	node* temp;
	node* b;
	 
	temp = new node;
	temp->data = "heloo";
	 
	b = temp;

	cout << "A:"<< temp->data << " : B:" << b->data << endl;
	
	temp->data = "yyy";
	cout << "A:"<< temp->data << " : B:" << b->data << endl;
}

this is because of line 17. you are setting a pointer equal to another pointer so whatever happens to temp will also happen to b. if you want only the data in b to be the same as temp but not actually point to the same object you would have to do something like

node* temp = new node;
node* b = new node;

temp->data = "hello";
b->data = temp->data;

thanks... just one more thing...
i am adding these node* to a list (of type node*) by using this command

list <node*> aList; //declaration for list
aList.push_back(b);

these statements are in a loop and when i push b in 2nd or 3rd iteration it will replace all existing data in list with latest pointer data... can you please help me out with some way around...

thanks heaps....

could you post the code you so far?

I your case, I do not see why is there a need for you to create a list of pointers. Why not consider having a list of the object node and only maintain one pointer to your list? Ain't it more efficient?

actually i have summarised my problem to make it easy to understand, whole program is more than 200 lines of code....

i am processing an infix expression data to a expression tree and then printing it as postfix and prefix notation and evaluating it. and it was outlined in specification to use the list of node* to store operator and operands in two different lists.

The problem is in the loop on each new iteration old pushed data in lists is also replaced to the latest value of temp.

#include <iostream>
#include <list>
 
using namespace std;
 
struct node {
	string data;
        node* left;
        node* right;
};

int main()
{
list <Node*> opStack;           // declared in specification
list <Node*> operandStack;    // declared in specification
string nextData;      //next data from an expression // expression e.g.  2+3*(4/2)
Node *temp;
	
temp = new Node;
temp->left = NULL;
temp->right = NULL;

while(nextData != NULL)
{
  if(isdigit(nextData))    //next data is digit
   {
       temp->s = nextData;
       operandStack.push_back(temp);
  }
  else
  {
       temp->s = nextData;
       opStack.push_back(temp);
  }  
}
}
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.