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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:06:24+00:00 2026-05-11T16:06:24+00:00

So, I have some code, kind of like the following, to add a struct

  • 0

So, I have some code, kind of like the following, to add a struct to a list of structs:

void barPush(BarList * list,Bar * bar)
{
    // if there is no move to add, then we are done
    if (bar == NULL) return;//EMPTY_LIST;

    // allocate space for the new node
    BarList * newNode = malloc(sizeof(BarList));

    // assign the right values
    newNode->val = bar;
    newNode->nextBar = list;

    // and set list to be equal to the new head of the list
    list = newNode; // This line works, but list only changes inside of this function
}

These structures are defined as follows:

typedef struct Bar
{
    // this isn't too important
} Bar;

#define EMPTY_LIST NULL

typedef struct BarList
{
    Bar * val;
    struct  BarList * nextBar;
} BarList;

and then in another file I do something like the following:

BarList * l;

l = EMPTY_LIST;
barPush(l,&b1); // b1 and b2 are just Bar's
barPush(l,&b2);

However, after this, l still points to EMPTY_LIST, not the modified version created inside of barPush. Do I have to pass list in as a pointer to a pointer if I want to modify it, or is there some other dark incantation required?

  • 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-11T16:06:24+00:00Added an answer on May 11, 2026 at 4:06 pm

    You need to pass in a pointer to a pointer if you want to do this.

    void barPush(BarList ** list,Bar * bar)
    {
        if (list == NULL) return; // need to pass in the pointer to your pointer to your list.
    
        // if there is no move to add, then we are done
        if (bar == NULL) return;
    
        // allocate space for the new node
        BarList * newNode = malloc(sizeof(BarList));
    
        // assign the right values
        newNode->val = bar;
        newNode->nextBar = *list;
    
        // and set the contents of the pointer to the pointer to the head of the list 
        // (ie: the pointer the the head of the list) to the new node.
        *list = newNode; 
    }
    

    Then use it like this:

    BarList * l;
    
    l = EMPTY_LIST;
    barPush(&l,&b1); // b1 and b2 are just Bar's
    barPush(&l,&b2);
    

    Jonathan Leffler suggested returning the new head of the list in the comments:

    BarList *barPush(BarList *list,Bar *bar)
    {
        // if there is no move to add, then we are done - return unmodified list.
        if (bar == NULL) return list;  
    
        // allocate space for the new node
        BarList * newNode = malloc(sizeof(BarList));
    
        // assign the right values
        newNode->val = bar;
        newNode->nextBar = list;
    
        // return the new head of the list.
        return newNode; 
    }
    

    Usage becomes:

    BarList * l;
    
    l = EMPTY_LIST;
    l = barPush(l,&b1); // b1 and b2 are just Bar's
    l = barPush(l,&b2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code like the following that I'm running in a debugger in
I have this kind of code in some applications (from microsoft) [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage( Microsoft.Naming,
I have some code like this in a winforms app I was writing to
I have some code like this: If key.Equals(search, StringComparison.OrdinalIgnoreCase) Then DoSomething() End If I
I have the following code. I'd like to make the proxy's type and url
imagine the following problem in grails you have some kind of audit trail domain
i have some code : manifest <uses-permission android:name=android.permission.ACCESS_FINE_LOCATION /> <uses-permission android:name=android.permission.ACCESS_COARSE_LOCATION /> activity fields
I have some code that creates a new event source: EventLog.CreateEventSource(Source, LogName); I know
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some code which collects points (consed integers) from a loop which looks

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.