0

I am having trouble storing a string in a linked list. This is the function that inserts a node to the list:

void insert_rec(rec_ptr *h_ptr, rec_ptr *t_ptr, int a, int b, int c, char* cs)
{   

    rec_ptr new_ptr;    
    new_ptr = rec_ptr( malloc( sizeof(REC) ) );

    if(new_ptr != NULL)
    {
        new_ptr->x    = a;
        new_ptr->y    = b;
        new_ptr->z    = c;
        new_ptr->c    = cs;
        new_ptr->next = NULL;

        if(*h_ptr == NULL){
            *h_ptr = new_ptr;
        }
        else{
            (*t_ptr)->next = new_ptr;
        }
        *t_ptr = new_ptr;
    }
    else
    {
        printf("%d %d %d not inserted. No memory available.\n",a,b,c);
    }
}

This is the function that reads input from an output file. I am inserting a string into the list as a char*. The fscanf() has read the string in correctly.

void read_from_input2(rec_ptr & hptr, rec_ptr & tptr)
{  

    fp3=fopen("input2.txt","r");

    if (fp3 == NULL)
        printf("Error: Couldn't open file: input2.txt\n");
    else
    {
        while(!feof(fp3))
        {
            int x,y,z;
            char c1[10];
            fscanf(fp3,"%d",&x);
            fscanf(fp3,"%d",&y);
            fscanf(fp3,"%d",&z);
            fscanf(fp3,"%s",c1);
            char *c2 = c1;
            insert_rec(&hptr,&tptr,x,y,z,c2);
        }
    }
    fclose(fp3);
}

This is the function where I am having problems. When I extract the data from the linked list, the variable c1 outputs garbage.

void write_to_output2(rec_ptr hptr)
{   

    fp4=fopen("output2.txt","w");

    if (fp4 == NULL)
        printf("Error: Couldn't open file: output2.txt\n");
    else
    {
        if(hptr == NULL){
            printf("List is empty.\n\n");
        }
        else{
            while(hptr != NULL)
            {
                int x,y,z;
                char *c1,*c2;
                x  = hptr->x;
                y  = hptr->y;
                z  = hptr->z;
                c1 = hptr->c;
                c2 = get_class(x,y,z);
                fprintf(fp4,"%d %d %d %s %s\n",x,y,z,c1,c2);
                hptr = hptr->next;
            }
        }
    }
    fclose(fp4);
}

If anyone can see my error please help me out. Thanks.

1 Answer 1

1
char c1[10];
/* ... */
char *c2 = c1;
insert_rec(&hptr,&tptr,x,y,z,c2);

The problem is c1 is on the stack of read_from_input2 and then you store a pointer to its contents. It will go out of scope when the while ends thus access to it will be invalid.

You'll want to strdup it (or equivalent).

char *c2 = strdup(c1);

/* or */

new_ptr->c = strdup(cs);

And don't forget to free it at some point.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.