• Advertisement
Sign in to follow this  

Pointer value not changing when I change the value it points to

This topic is 4366 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

typedef struct _BST_node {
	void* key;
	void* data;
	struct _BST_node *left, *right, *parent;
} BST_node;

typedef struct _Heap {
	int numObjs, maxSize;
	BST_node** array;
} Heap;

void addToHeap(Heap** heap, void* key, void* item) {
	int pos=0;
	Heap* h2 = *heap;

	pos = h2->numObjs;
	h2->array[pos]->left = (*heap)->array[2*pos+1];
}
When I change (*heap)->array[2*pos+1], shouldn't the value that h2->array[pos]->left points to also change? If not, how can I get it to?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Sfpiano
When I change (*heap)->array[2*pos+1], shouldn't the value that h2->array[pos]->left points to also change?


No.

Quote:
If not, how can I get it to?



h2->array[pos]->left = &((*heap)->array[2*pos+1]);

Share this post


Link to post
Share on other sites
The datatype of the following line

h2->array[pos]->left

is of type
struct _BST_node
. When you assign in the value of (*heap)->array[2*pos+1] you are doing an assignment between two structs - this is a member-by-member copy.

If you want h2->array[pos]->left to change when (*heap)->array[2*pos+1] changes then you'll need to make left a pointer to a _BST_node and make left point to (*heap)->array[2*pos+1]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement