segmentation fault when trying to deference pointer : C

sdinesh94

I was trying to implement circular queue functionality. I am a C++ coder and I found it surprising that in C, struct cannot have member functions. Anyway this is my implementation:-

#include <stdio.h>
#include <stdlib.h>



struct node
{
    int nvalue;
    struct node *next;
};

struct CLlist
{
    struct node* head;
    struct node* tail;
    int size;
};

void insert(struct CLlist *l,int num)
{
    struct node *n=malloc(sizeof(struct node));
    n->nvalue=num;
    n->next=NULL;

    if((l->head==l->tail)==NULL)
    {
        l->head=l->tail=n;
    }
    else if(l->head==l->tail && l->head!=NULL)
    {
        l->head->next=n;
        l->tail=n;
        l->tail->next=l->head;
    }
    else
    {
        l->tail->next=n;
        l->tail=n;
        l->tail->next=l->head;
    }
    l->size++;
}

void print(struct CLlist *l)
{
    int idno=1;
    printf("printing the linked list with size as %d\n",l->size);
    struct node *cptr;
    for(cptr=(l->head);cptr!=(l->tail);cptr=cptr->next)
    {
        printf("The idno is %d and the number is %d\n",idno,cptr->nvalue);
        idno++;
    }
    //this is to print the last node in circular list : the tail node
    idno++;
    cptr=cptr->next;
    printf("The idno is %d and the number is %d\n",idno,cptr->nvalue);
}


int main()
{
    struct CLlist a;
    struct CLlist *l;
    l=&a;

    insert(l,2);
    insert(l,5);
    insert(l,7);
    insert(l,10);
    insert(l,12);
    print(l);


    return 0;
}

I get segmentation fault in the line

printf("The idno is %d and the number is %d\n",idno,cptr->nvalue);

why does the error occur? I guess I am not passing l by pointer by value (passing pointers as by value) properly. could somebody help me in pointing out where I am going wrong?

Thanks

Joel C

Your code has two issues, the first one more serious.

Your first issue is that the head and tail members of your CLlist structure are not being initialized to NULL, which can (non-deterministically) keep any real data from being stored in your structure. This can be fixed by adding the following 2 lines in main just before the first insert call:

l->head = NULL;
l->tail = NULL;

Your second problem is in this line:

if((l->head==l->tail)==NULL)

While it looks like this is comparing both l->head and l->tail to NULL, it's actually comparing l->head to l->tail, and then comparing that boolean result to NULL, which is effectively 0. The line should be changed to:

if((l->head == NULL) && (l->tail == NULL))

This will individually test both the head and tail pointers, and will only take that branch if they are both NULL.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

segmentation fault when trying to deference pointer : C

From Java

Segmentation Fault when trying to access pointer to Node

From Dev

Segmentation fault when trying to access pointer in struct

From Dev

Segmentation fault when trying to access pointer in struct

From Dev

Segmentation fault when trying to clone a function (in C)

From Dev

Segmentation fault when trying to clone a function (in C)

From Dev

Passing pointer to function causes segmentation fault when trying to retrieve value

From Dev

segmentation fault when trying to dereference a dynamically allocated pointer

From Dev

Segmentation fault in C. Trying to swap two values using pointer

From Dev

Segmentation fault in C. Trying to swap two values using pointer

From Dev

C array pointer segmentation fault

From Dev

Trying to deference a pointer back into a structure

From Dev

C segmentation fault when using pointer to structure in a linked list

From Dev

C Segmentation fault in a Circularly Linked List when setting next pointer

From Dev

C segmentation fault when using pointer to structure in a linked list

From Dev

Segmentation fault when trying to allocate memory for a string via a pointer-to-pointer function call

From Dev

Segmentation fault in C when trying to import header file and use it

From Dev

Segmentation fault in C when trying to reference first element in char *

From Dev

Segmentation fault in C when trying to import header file and use it

From Dev

Pointer-to-a-pointer in C throws segmentation fault

From Dev

Pointer Pointer leads to Segmentation Fault in C

From Dev

Segmentation Fault when deleting a pointer in vector

From Dev

Segmentation fault when passing pointer to function

From Dev

Segmentation fault when trying to free memory

From Dev

"Segmentation fault" when trying to print an integer

From Dev

Odd C pointer segmentation fault errors

From Dev

Why this pointer got a Segmentation fault C?

From Dev

segmentation fault using char pointer (C)

From Dev

Segmentation fault error with pointer array in C

Related Related

  1. 1

    segmentation fault when trying to deference pointer : C

  2. 2

    Segmentation Fault when trying to access pointer to Node

  3. 3

    Segmentation fault when trying to access pointer in struct

  4. 4

    Segmentation fault when trying to access pointer in struct

  5. 5

    Segmentation fault when trying to clone a function (in C)

  6. 6

    Segmentation fault when trying to clone a function (in C)

  7. 7

    Passing pointer to function causes segmentation fault when trying to retrieve value

  8. 8

    segmentation fault when trying to dereference a dynamically allocated pointer

  9. 9

    Segmentation fault in C. Trying to swap two values using pointer

  10. 10

    Segmentation fault in C. Trying to swap two values using pointer

  11. 11

    C array pointer segmentation fault

  12. 12

    Trying to deference a pointer back into a structure

  13. 13

    C segmentation fault when using pointer to structure in a linked list

  14. 14

    C Segmentation fault in a Circularly Linked List when setting next pointer

  15. 15

    C segmentation fault when using pointer to structure in a linked list

  16. 16

    Segmentation fault when trying to allocate memory for a string via a pointer-to-pointer function call

  17. 17

    Segmentation fault in C when trying to import header file and use it

  18. 18

    Segmentation fault in C when trying to reference first element in char *

  19. 19

    Segmentation fault in C when trying to import header file and use it

  20. 20

    Pointer-to-a-pointer in C throws segmentation fault

  21. 21

    Pointer Pointer leads to Segmentation Fault in C

  22. 22

    Segmentation Fault when deleting a pointer in vector

  23. 23

    Segmentation fault when passing pointer to function

  24. 24

    Segmentation fault when trying to free memory

  25. 25

    "Segmentation fault" when trying to print an integer

  26. 26

    Odd C pointer segmentation fault errors

  27. 27

    Why this pointer got a Segmentation fault C?

  28. 28

    segmentation fault using char pointer (C)

  29. 29

    Segmentation fault error with pointer array in C

HotTag

Archive