Search code examples
clistinitializationstructuresingly-linked-list

C: problem with operator '->', compile but return error. There are main.c and .h, .c files


I'm new to the site. I use CodeBlocks. I am working with lists in C and I have to create a program with several functions but which has a header to declare them and another .c file to implement them, then call everything from main.c. The problem arises when I compile the code, insert n and then pressing enter as soon as it enters the for loop it goes into error. I believe there is a problem with the '->' operator. Could you help me please?

    //main.c
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <list.h>
    
    int main()
    {
        struct element *list;
        list = create_list();
    
        return 0;
    }
    //list.h
    
    #ifndef LIST_H_INCLUDED
    #define LIST_H_INCLUDED
    
    
    
    #endif // LIST_H_INCLUDED
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    struct element {
       int number;
       struct element *pointer;
    };
    
    struct element *create_list();
    //list.c
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include <list.h>
    
    int n = 0;
    
    struct element *create_list() {
    
       struct element *p, *ptr;
       int i;
    
       printf("Insert the number of element of the list... ");
       scanf("%d", &n);
    
       if (n==0) {
    
        p = NULL;
    
       } else {
    
        p = (struct element*)malloc(sizeof(struct element));
        p->number = 0;
        for(i=1; i<=n; i++) {
    
            //printf("ciao\n");
            ptr->pointer = (struct element *)malloc(sizeof(struct element));
            ptr = ptr->pointer;
            ptr->number = i;
        }
    
        ptr->pointer = NULL;
       }
    
    return(p);
    }

Solution

  • This declaration

    struct element *p, *ptr;
    

    declares two variables with automatic storage duration that have indeterminate values because they were not initialized explicitly.

    So in this for loop

        for(i=1; i<=n; i++) {
    
            //printf("ciao\n");
            ptr->pointer = (struct element *)malloc(sizeof(struct element));
            ptr = ptr->pointer;
            ptr->number = i;
        }
    

    you are using the uninitialized pointer ptr trying to access the data member pointer of a non-existent object.

    It seems before the loop you mean the following assignment

        p = (struct element*)malloc(sizeof(struct element));
        p->number = 0;
        ptr = p;  // <===
        for(i=1; i<=n; i++) {
    
            //printf("ciao\n");
            ptr->pointer = (struct element *)malloc(sizeof(struct element));
            ptr = ptr->pointer;
            ptr->number = i;
        }
    

    Pay attention to that it is a bad idea to declare the global variable n that moreover has a signed integer type instead of an unsigned integer type.

    If the user will enter a negative number the function will return an unexpected result of a pointer to the node with the value equal to 0.

    Also place declarations in the header between #ifndef and #endif

    #ifndef LIST_H_INCLUDED
    #define LIST_H_INCLUDED
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    struct element {
       int number;
       struct element *pointer;
    };
    
    struct element *create_list();    
    
    #endif // LIST_H_INCLUDED
    

    And the header malloc.h is not a standard C header. The memory allocation functions are in the header <stdlib.h>. Your program should work without the header <malloc.h>. So remove it.