Search code examples
cstructappendsingly-linked-listfunction-definition

Problem entering values to a struct with double pointers


I have to use double pointer in the function for filling elements to the struct (the function must be void). But it doesn't print anything. I assume that the problem is with passing the right address but cannot find it.

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

typedef struct nums{
    int num;
    struct nums *ptr;
}sNums;

void addRecords(sNums** head);
sNums* createRecord();
void prinrecords(sNums* head);

int main(int argc, char const *argv[])
{
    sNums* head=NULL;
    printf("%d\n", &head);
    for (int i = 0; i < 3; ++i)
    {
        addRecords(&head);
    }
    system ("pause");
}

This is the function for printing the stored elements:

void prinrecords(sNums* head){
    while(head!=NULL){
        printf("{%d} ", head->num);
        head=head->ptr;
    }
}

Here is the function for adding elements using a double pointer:

void addRecords(sNums** head){
    sNums* temp_new=createRecord();
    sNums* fst_position;
    fst_position=*head;
    printf("%d\n", fst_position);
    if (fst_position == NULL)
    {
        fst_position=temp_new;
        return ;
    }
    while(fst_position->ptr!=NULL){
    fst_position=fst_position->ptr;
    }
    fst_position->ptr=temp_new; 
}

sNums* createRecord(){
    sNums *new=(sNums*)malloc(sizeof(sNums));
    printf("Enter Number: ");
    scanf("%d", &new->num);
    new->ptr=NULL;
    return new;
}

Solution

  • This code snippet

    fst_position=*head;
    //...
    if (fst_position == NULL)
    {
        fst_position=temp_new;
        return ;
    }
    

    does not change the passed by reference the head pointer. It changes the local variable fst_position.

    The function can be defined the following way

    void addRecords(sNums** head)
    {
        while ( *head != NULL ) head = &( *head )->ptr;
    
        *head = createRecord();
    }
    

    That is all. Only two statements. :)

    Though in general the design of the functions is not good. For example entering numbers that will be added to the list should be outside the function createRecord.

    Moreover allocation of memory can fail. In this case your program will have undefined behavior.

    Below there is a demonstrative program that shows how your functions can be redesigned.

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct nums
    {
        int num;
        struct nums *ptr;
    } sNums;
    
    int addRecords(sNums** head, int num );
    sNums* createRecord();
    void prinrecords( const sNums* head );
    
    sNums * createRecord( int num ) 
    {
        sNums *node = malloc( sizeof( sNums ) );
    
        if ( node != NULL )
        {
            node->num = num;
            node->ptr = NULL;
        }
    
        return node;
    }
    
    int addRecords( sNums** head, int num )
    {
        sNums *node = createRecord( num );
        int success = node != NULL;
    
        if ( success )
        {
            while ( *head != NULL ) head = &( *head )->ptr;
    
            *head = node;
        }
    
        return success;
    }
    
    void prinrecords( const sNums *head )
    {
        for ( ; head != NULL; head = head->ptr )
        {
            printf( "%d -> ", head->num );
        }
        puts( "null" );
    }
    
    int main(void) 
    {
        sNums* head = NULL;
        const size_t N = 10;
    
        for ( size_t i = 0; i < N; ++i )
        {
            int num;
    
            printf( "Enter a number: " );
            scanf( "%d", &num );
    
            addRecords( &head, num );
        }
    
        prinrecords( head );
    
        return 0;
    }
    

    The program output might look like

    Enter a number: 0
    Enter a number: 1
    Enter a number: 2
    Enter a number: 3
    Enter a number: 4
    Enter a number: 5
    Enter a number: 6
    Enter a number: 7
    Enter a number: 8
    Enter a number: 9
    0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null