I'm trying to pass a pointer to a queue into the createQueue function:
void createQueue(struct pqueue *queue){
queue = malloc( sizeof(struct pqueue) );
queue->root = malloc(sizeof(struct node));
queue->root->next = 0;
queue->root->taskID = 12;
queue->root->priority = 5000;
}
I also try to add to the newly created queue like this:
void add(struct pqueue *queue, int taskID, int priority){
struct node *conductor;
conductor = queue->root;
if ( conductor != 0 ) {
while ( conductor->next != 0)
{
conductor = conductor->next;
}
}
conductor->next = malloc( sizeof(struct node) );
conductor = conductor->next;
if ( conductor == 0 )
{
printf( "Out of memory" );
}
/* initialize the new memory */
conductor->next = 0;
conductor->taskID = taskID;
conductor->priority = priority;
}
from the main function:
int main()
{
struct pqueue *queue;
createQueue(queue);
add(queue, 234093, 9332);
}
...but I keep segfaulting. Any reason why this keeps happening?
EDIT:
The structs for pqueue and node are like this:
struct node {
int taskID;
int priority;
struct node *next;
};
struct pqueue{
struct node *root;
};
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…