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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:35:46+00:00 2026-06-02T03:35:46+00:00

Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> typedef struct node { char*

  • 0

Code:

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

typedef struct node {
    char* identifier;
    char* expression;
    struct node* next;
} node;

void add(node** database, char* _identifier, char* _expression, int * size) {
    node* tmp = (node*) malloc (sizeof(node));
    tmp->identifier = _identifier;
    tmp->expression = _expression;
    tmp->next = *database;
    *database = tmp;
    (*size)++;
    printf("Added: %s : %s\n", _identifier, _expression); fflush(NULL);
}

void show(node* database, int size) {
    if (database == NULL) {
        printf("Database empty\n");
    }
    else {
        node* tmp = database;
        printf("Identifier list (%d):\n", size); fflush(NULL);
        while (tmp != NULL) {
            printf("%s : \"%s\" \n", tmp->identifier, tmp->expression);fflush(NULL);
            tmp = tmp->next;
        }
    }
}

int main() {
    node* database = NULL;
    int size = 0;
    add(&database, "a1", "abc", &size);
    add(&database, "a2", "def", &size);
    add(&database, "a3", "ghi", &size);

    char identifier[20];
    char expression[1000];
    int i;
    for (i = 0; i<3; i++) {
        scanf("%s %s", identifier, expression);
        add(&database, identifier, expression, &size);
    }
    show(database, size);
    printf ("Bye!");
    return 0;
}

The “Add” function works well when called manually, but it doesn’t work inside the loop, with data read from stdin. Below is the result from one run:

Added: a1 : abc
Added: a2 : def
Added: a3 : ghi
a4 mno
Added: a4 : mno
a5 ghi
Added: a5 : ghi
a6 kml
Added: a6 : kml
Identifier list (6):
a6 : "kml" 
a6 : "kml" 
a6 : "kml" 
a3 : "ghi" 
a2 : "def" 
a1 : "abc" 
Bye!

As you can see the node a1, a2, a3 were added correctly into the list. However, a4 and a5, after correctly added to the list, were changed to all “a6” after a6 is added.
It took me one day already but I couldn’t figure it out. Anybody?

  • 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-06-02T03:35:48+00:00Added an answer on June 2, 2026 at 3:35 am

    Your problem is that they are all pointing to the same pointer: identifier. (also expression). (Whereas the first 3 adds are pointing to separate const char*s statically initialised.)

    To resolve this you need to dynamically allocate space for each new element you add (e.g. with malloc) and release them (e.g. with free) when you remove them.

    e.g:

    void add(node** database, char* _identifier, char* _expression, int * size) {
       node* tmp = (node*) malloc (sizeof(node));
       tmp->identifier = (char *)malloc(strlen(_identifier));
       strcpy(tmp->identifier, _identifier);
       tmp->expression = (char *)malloc(strlen(_expression));
       strcpy(tmp->expression, _expression);
       tmp->next = *database;
       *database = tmp;
       (*size)++;
       printf("Added: %s : %s\n", _identifier, _expression); fflush(NULL);
    }
    

    You must ensure that these resources are correspondignly released at some point or you will have a memory leak.

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

Sidebar

Related Questions

I have this snippet of C code: #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct Date {
I have a code: #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> typedef struct
My code #include <stdio.h> #include <stdlib.h> #include <ctype.h> void getData(short int *number, char *string)
I have the following code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> char
This is the code #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h>
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<stdbool.h> char** parser(char *message) { char a[9][256]; char* tmp =message; bool
Here is my code #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
Test the following code: #include <stdio.h> #include <stdlib.h> main() { const char *yytext=0; const

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.