Setting value to a double struct pointer

Vineet Patel

So, I am implementing a linked-list based queue and I put the node information in a structure and I put the head and the tail of the queue in a structure. When I try to add a node, I have to use -> twice, and I ended up getting a segmentation fault error.

Now my code is part of a larger program with user input and multiple options and stuff but I simplified it down for ease.

typedef enum statucEnum {CALLED_AHEAD, WAITING} status; 
typedef struct nodeStruct{
    char* name; 
    int groupSize; 
    status inStatus;//in resturant status 
    struct nodeStruct* next;
}Node;

//structure to encapsulate the head of the queue along with the tail 
typedef struct headStruct{
    Node* head; 
    Node* tail;
}Queue; 

void intializeQueue(Queue* queue){
    queue->head = NULL; 
    queue->tail = NULL; 
}

int main(){
    Queue queue; 
    intializeQueue(&queue);
    //program ask what the user wants to do, and it decides to add to the queue
    doAdd(&queue);
}

void doAdd(Queue* queue){
    //usually the program would ask the user to input a age and name 
    int age = 4;
    name = "v";
    Node* newNode; 
    newNode = malloc(sizeof(Node));
    newNode->groupSize = size; 
    newNode->name = name;
    newNode->inStatus = WAITING; 
    addToList(queue, newNode); 
}

When I use valgrind, it tells me that the segmentation fault is in this code segment

void addToList(Queue* queue, Node* node){   
    printf("Address of parameter: %p", node);
    if (queue->head == NULL){
        queue->head->next = node; \\this is where the error occurs 
        queue->tail->next = node; 
    }else{
        queue->tail->next = node; 
        queue->tail = node; 
    }
} 

More specifically at the line of queue->head->next = node

I can't seem to figure out what I am doing wrong.

Salman

When you head is NULL then how can you set queue->head->next = node;. First of all set you head value then you can update your head next point. So see following code

void addToList(Queue* queue, Node* node){   
    printf("Address of parameter: %p", node);
    if (queue->head == NULL){
        queue->head = node; \\this is where the error occurs 
        queue->tail = node; 

    }else{
        queue->tail->next = node; 
        queue->tail = node; 
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Assign value to member of struct using double pointer

double pointer to struct inside struct

Double pointer to struct

Assign a Struct Pointer to Double Pointer Struct

Accessing struct from double pointer

Using free with double pointer to a struct

Freeing struct that cointains double pointer

Setting value to struct pointers

Value of pointer inside an struct

Set the value of a pointer in a struct

Setting a pointer to struct to equal to another pointer to struct returned by a function?

Improvements to dynamically allocate memory for a double pointer to struct inside a pointer to struct

Trouble setting the value of a struct in reflection

Setting existing struct value in Elixir?

pass pointer value into a struct in c

Struct value of pointer, array and slice

reflect, assign a pointer struct value

Update pointer value in struct automatically

Access to another struct by value or by pointer

Pointer to Struct Value Memory Leak

In C, I'm having trouble getting the pointed value of a member struct(a double pointer). The value is lost outside the function assigning the value

Malloc'ing a double pointer structure inside a struct?

First element in a double pointer to struct is jiberrish

Proper way of deallocation of double pointer to struct

How to free a tree struct using double pointer?

Initialize matrix (double pointer) member of struct

Swift define double pointer for struct defined in c

How to pass/access a double pointer to/from a struct?

How to free memory allocated on double pointer struct