[c] Dequeue to a pointer?

13lade619

is now a game developer :)
Reaction score
399
I want to dequeue to tempnode, but it needs to be a pointer.

Code:
Node *tempnode;
	
	while(!IsQueueEmpty(&nodeQueue)){
		Dequeue(&nodeQueue, tempnode);
		printf("%s\n",tempnode->info);
	};

tempnode isnt getting the correct values..

Code:
struct node {
	char info[100];
};
typedef struct node Node;

struct qnode {
	Node *info;
	struct qnode *link;
};
typedef struct qnode QueueNode;

struct queue {
	QueueNode *front;
	QueueNode *rear;
};
typedef struct queue Queue;

void Dequeue(Queue *Q, Node *n) {
	QueueNode *alpha;
	
	n = Q->front->info;
	
	alpha = Q->front;
	Q->front = Q->front->link;

	free(alpha);
}

Specifically, I need Q->front->info out of the Dequeue function into a pointer variable..
 

13lade619

is now a game developer :)
Reaction score
399
the Enqueue?
Code:
void Enqueue(Queue *Q, Node *n) {
	QueueNode *alpha;
	alpha = (QueueNode*) malloc(sizeof(QueueNode));
	
	alpha->info = n;
	alpha->link = NULL;
	if(Q->front==NULL) {
		Q->front = alpha;
		Q->rear = alpha;
	}
	else {
		Q->rear->link = alpha;
		Q->rear = alpha;
	}
}

The values are ok inside the Enqueue and inside the dequeue.. I just need a way to store what I dequeued into a pointer.

I need to dequeue by reference.

EDIT

Nvm. I got it to work.. It turns out I can do *FunctionName.

Code:
Node *Dequeue(Queue *Q) {
	QueueNode *alpha;
	Node *n = Q->front->info;

	alpha = Q->front;
	Q->front = Q->front->link;

	free(alpha);
	return n;
}
 

UnknowVector

I come from the net ... My format, Vector.
Reaction score
144
It has been a while since I've written any c++, but as far as i can see, it looks like you've confused some pass-by-value semantics.

Code:
void Dequeue(Queue *Q, Node *n) {
	QueueNode *alpha;
	
	n = Q->front->info;
	
	alpha = Q->front;
	Q->front = Q->front->link;

	free(alpha);
}

For the sake of argument, replace the n argument. Instead of taking a pointer to a node, take an integer.

Code:
void Dequeue(Queue *Q, [COLOR=red]Int n[/COLOR]) {
	QueueNode *alpha;
	
	n = [COLOR=red]blah[/COLOR]
	
	alpha = Q->front;
	Q->front = Q->front->link;

	free(alpha);
}

See the problem? Your changing your copy of the pointer, but that doesn't affect the caller. You can use pointers to achieve pass-by-reference, but the only thing that lets you change is the data referenced, not the pointer itself.

If you want to change the pointer itself, tempnode in the caller, you need to give a pointer to it, i.e., a pointer to a pointer.

Try this:


Code:
Node *tempnode;
	
	while(!IsQueueEmpty(&nodeQueue)){
		Dequeue(&nodeQueue, [COLOR=red]&[/COLOR]tempnode);
		printf("%s\n",tempnode->info);
	};

void Dequeue(Queue *Q, Node * [COLOR=red]*[/COLOR]n) {
	QueueNode *alpha;
	
	[COLOR=red]*[/COLOR]n = Q->front->info;
	
	alpha = Q->front;
	Q->front = Q->front->link;

	free(alpha);
}

Again, been a while since I've used c++, and I don't even have a c++ compiler installed right now. I might have made a syntax error in my writing of "Node * * n".

Edit: Also, I've never written straight c.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top