Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 647203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:41:48+00:00 2026-05-13T21:41:48+00:00

this one is not homework, it just the fact that i’ve been out of

  • 0

this one is not homework, it just the fact that i’ve been out of school for more than 20 years and I NEVER had to use linked lists for anything at all. So i’m out of my element here.
anyway, I have

struct Data_Struct {
    char *Name;
    char *Task;
    char *Pos;
    struct Data_Struct *Next;
};
typedef struct Data_Struct MyData;

Which is a pretty simple struct for a linked list.

As you can see the data is stored in char* and when I populate the list I copy the pointers there.
All is well. The problem begins when those pointers get overwritten and I lose the original data. I tried copying the data into local char* malloc’ed on every “list_add()” but I keep on either crashing or losing data.
Yes, I do allocate enough and yes, I do copy the data and check that it’s copied correctly. I tried with memcpy, strcpy, etc ,etc.

I’m out of things to try so my question is, how do I make sure that the data I will be adding to the list is local. how do I copy those char* going into the list into local variables.

while an explanation on how to do this and why my code is bad is OK, I prefer code with the explanation.
Thank you.

Jess.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:41:49+00:00Added an answer on May 13, 2026 at 9:41 pm

    You don’t want to “copy into local”, you want to do as you say you’ve done; allocate fresh memory using malloc(), store the data there, and save the pointers in the list node.

    If you have it, you can use strdup() to combine the malloc()+strcpy() into one call.

    It’s hard to understand under what circumstances you experience that the data is overwritten, what are you doing to the list node(s) that cause this?

    A basic prepend ought to look like this, for your node definition:

    MyData * list_prepend(MyData* head, const char *name, const char *task,
                      const char *pos)
    {
      MyData *node = malloc(sizeof *node);
      node->Name = strdup(name);
      node->Task = strdup(task);
      node->Pos = strdup(pos);
      node->Next = head;
      return node;
    }
    

    Obviously this lacks error handling (malloc() and strdup() can both fail). Note that it’s a prepend, and that the new head of the list is returned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is not a homework problem. This questions was asked to one of my
I got this exercise, is not a homework , I just trying to solve:
[FYI, this is not homework — I guess everyone thinks that because I made
(This is not a CS class homework, even if it looks like one) I'm
This is not homework, just a question about my code (I'm learning C++). I
I need a good OLEDate java implementation, and this one does not seem to
My question is not like this one: Browser-independent way to detect when image has
I am not able to make a clear decision on this one. I am
I'm a little stumped on this one and I'm also not still on 1.8
I know there are questions similar to this one, but I've not found a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.