C - Segmentation fault when checking if a pointer is NULL

garjted

so got this segmentation fault every time I tried to check if a pointer variable is a NULL. The error is from these lines of code in add function:

if (it->head == NULL){
        printf("worksfine");
    }

This is the whole code that I have:

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

typedef struct Node{
    int val;
    struct Node *prev;
    struct Node *next;
} node;

typedef struct IteratorInt{
    node *head;
    node *tail;
    node *last;
} IteratorInt;

IteratorInt IteratorIntNew(){
    IteratorInt *listint;
    listint = malloc(sizeof(IteratorInt));
    listint->head = NULL;
    listint->tail = NULL;
    listint->last = NULL;
    printf("address made %u\n", listint);
    return *listint;
}

int add(IteratorInt *it, int v){
    node *n;
    n->val = v;
    n->next = NULL;
    n->prev = NULL;
    printf("func works\n");
    printf("n %d\n", n->val);
    printf("address %u", it);
    it->head = n;
    printf("result %d", it->head->val);
    if (it->head == NULL){
        printf("worksfine");
    }
   /* if (it->head == 0){
       it->head = n;
    }
    if (it->tail == 0){
        it->tail = n;
    }
    if (it->last == 0){
        it->last = n;
    }*/

    return 1;
}

int main() {
    IteratorInt lit = IteratorIntNew();
    printf("works %u", &lit);
    add(&lit, 10);

    /*printf("Node value %d\n", lit.head.val);
    add(&lit, 15);
    printf("Node value %d", lit.tail.val);*/
    return 0;
}

Can you tell me what's wrong with it? and how to solve that? Thanks a lot.

Roun

In your add function, the variable n is an uninitialized pointer. So it is not the problem of checking it->head.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

C - store to null pointer error, segmentation fault

segmentation fault when trying to deference pointer : C

A Segmentation Fault occurs when calling free() on a null pointer

Segmentation fault when passing by pointer

C++ Segmentation Fault From Null Pointer Solution?

Segmentation Fault when assigning value to a pointer C++

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

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

Pointer Pointer leads to Segmentation Fault in C

expected segmentation fault while incrementing NULL pointer

Segmentation fault when trying to access pointer in struct

Segmentation fault when accessing array via pointer

Segmentation fault when returnin pointer in yacc

SIGSEGV Segmentation Fault when dereferencing a pointer

Segmentation Fault when access to a pointer of struct in struct

Segmentation Fault when trying to access pointer to Node

Segmentation fault when passing pointer to function

Segmentation fault when passing a pointer into function

segmentation fault when accessing pointer element

Segmentation Fault With Char Array and Pointer in C on Linux

Why this pointer got a Segmentation fault C?

strsep() with char pointer gives segmentation fault in c

segmentation fault using char pointer (C)

C array of values, pointer segmentation fault

Segmentation fault error with pointer array in C

c++ Segmentation Fault using struct pointer

Odd C pointer segmentation fault errors

(C) Getting segmentation fault on pointer strcpy

zsh: segmentation fault && runtime error: member access within null pointer of type 'struct ListNode' [solution.c]