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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:57:31+00:00 2026-05-27T15:57:31+00:00

When I run the program below, it prints one: 1, instead of one :

  • 0

When I run the program below, it prints “one: 1”, instead of “one : 1, two: 2”, as I had expected. Anyone know what’s going on here? I am trying to create a function that will allow me to create as many linked lists as possible instead of just declaring global heads.

struct Node {
  int value;
  char label[10];
  node *next;
};
typedef struct Node node;

int add(int data, char name[], node *head) {
  node *newNode = (node *)malloc(sizeof(node));
  if (newNode != NULL) {
    newNode->value = data;
    strcpy(newNode->label, name);
    newNode->next = head;
    head = newNode;
  }
}

node* createNewLinkedList(int d, char *name) {
  node *newNode = (node *)malloc(sizeof(node));
  newNode->value = d;
  strcpy(newNode->label, name);
  newNode->next = NULL;
  return newNode;
}

int main() {
  node *head1 = createNewLinkedList(1, "one");
  add(2, "two", head1);
  iterate(head1);
}
  • 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-27T15:57:32+00:00Added an answer on May 27, 2026 at 3:57 pm

    You are passing a pointer to the node. Like all parameters in C, it is passed by value, therefore head = newNode; has no effect in the caller.

    You need to change the signature to accept node **head, and add a level of indirection in order to make the changes in add be reflected in the main.

    int add(int data, char name[], node **head) {
        node *newNode = (node *)malloc(sizeof(node));
        if (newNode != NULL) {
            newNode->value = data;
            strcpy(newNode->label, name);
            newNode->next = *head;
            *head = newNode;
        }
    }
    

    Of course you’ll need to pass &head1 to the add method: add(2, "two", &head1);

    P.S. Since you are adding {2,”two”} to the front of the list, your output will be “two: 2, one : 1”

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

Sidebar

Related Questions

In the program below, I am trying to calculate the distance between two points.
I am trying run a program from a qmake .pro file which modifies the
consider the program below char str[5]; strcpy(str,Hello12345678); printf(%s,str); When run this program gives segmentation
When I run the below program I am getting the following errors: (any ideas?)
I run the program and all I see is white space below Netbeans. At
[Updated]: Answer inline below question I have an inspecting program and one objective is
I am trying to run a batch script from within a c sharp program
The program below prints each character written on standard in, but only after a
I want to design a c# program which can run program a 3rd exe
To run a program with custom env settings, we do this on Linux $

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.