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 7705861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:58:04+00:00 2026-05-31T23:58:04+00:00

I have the following structure and function that adds things to the structure: struct

  • 0

I have the following structure and function that adds things to the structure:

struct scoreentry_node {
    struct scoreentry_node *next;
    int score;
    char* name;    
}
;

typedef struct scoreentry_node *score_entry;

score_entry add(int in, char* n, score_entry en) {      
   score_entry r = malloc(sizeof(struct scoreentry_node));
   r->score = in;
   r->name = n;
   r->next = en;  
   return r;   
}

i have input that take it in the following main file:

int score;
char name[];

int main(void) {
score_entry readin = NULL;

while(1)
{

        scanf("%s%d", name, &score);
        readin = add(score, name, readin);
       // blah blah

I dont know why but when input a name it gets added to readin, but when i input another name all the names in readin have this new name

for example:

input:
bob 10 
readin = 10 bob NULL

jill 20
readin = 20 jill 10 jill NULL

I dont know why bob disappears… any reason why it does that ?

  • 1 1 Answer
  • 0 Views
  • 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-31T23:58:05+00:00Added an answer on May 31, 2026 at 11:58 pm

    You’re just storing a pointer to the name string, not a copy of the string itself. Try using strdup:

    r->name = strdup(n);
    

    Just be sure to free(r->name) when freeing a node.

    An alternative would be to change char* name to char name[1], and do something like this:

    score_entry add(int in, char* n, score_entry en) {      
       score_entry r = malloc(sizeof(struct scoreentry_node) + strlen(n));
       r->score = in;
       strcpy(r->name, n);
    
       [..]
    

    This saves allocating a separate chunk of memory for the string, but note that in that case name must be at the end of your structure.

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

Sidebar

Related Questions

I have the following structure: struct hashItem { char userid[8]; char name[30]; struct hashItem
Say we have the following structure: struct Tree { string id; int numof_children; Tree
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have the following function that prunes a tree data structure : public static
I have a plugin with the following structure: (function($) { $.fn.drawFieldsTable = function(options) {
I have the following function : var appendStructure = { init : function(wrapper,structure,cls,callback) {
I have the following DOM structure (unordered list), that I would like to sort
I have a C project that has the following structure Main/ Makefile.am bin/ src/
I have the following structure that works and renders a direct x scene to
Given the following data structure var things = [{ name: thing1, sex: male}, {

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.