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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:52:42+00:00 2026-05-27T00:52:42+00:00

I have a linked list and a setter function. struct my_struct { int value;

  • 0

I have a linked list and a setter function.

struct my_struct {
    int    value;
    int  type;
    char   *name;
    struct my_struct *next;
};

struct my_struct *setValue(struct my_struct *s, char *name, int b) {
    if(s!=NULL) {
        while(s != NULL) {
            if( strcmp(s->name,name) == 0) {
                s->value = b;
            }
            s=s->next;
        }
        return s;
    }
    return NULL;
}

Here, name is the search keyword and b is new value of s->value. Why s->value cannot change? After that function, the output is weird. I can’t understand, what happened.

  • 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-27T00:52:43+00:00Added an answer on May 27, 2026 at 12:52 am

    You need to be testing the strings equality with strcmp, as seen below. In your code, you’re testing if two pointers are equal [related post].

    #include <string.h>
    
    if(strcmp(s->name, name) == 0) { // if both strings are equal
        s->value = b;
    }
    

    The location of your return statement is interesting. You’re returning the address of the last item that was changed, which may be undesired.

    As per @Matthew Iselin’s comment, change your loop to the following:

    while(s != NULL) {
        ...
    }
    

    In case you are setting the root node to the return value of the function, s will always be NULL after looping through the linked list, therefore the function will always return NULL.

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

Sidebar

Related Questions

I have a linked list structure: struct SomeLinkedList { const char* bar; int lots_of_interesting_stuff_in_here;
I have the following array structure (linked list): struct str_pair { char ip [50]
I have the following function defined inside my linked list class. The declaration in
Basically, I have a Linked List which implements a display() function that simply loops
I have a simple linked list. The node contains a string (value) and an
I have a linked list of Nodes, each Node is defined as: struct Node
I have a linked list of arrays (struct at bottom of post) Each array
I have a Linked List that holds contact information(name, date of birth, etc...). I
In my app I have a Linked List (type List), every time the user
Suppose, I have a singly linked list and its basic building block is, struct

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.