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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:50:05+00:00 2026-06-17T20:50:05+00:00

So i have my definition of the list as a global variable: typedef struct

  • 0

So i have my definition of the list as a global variable:

typedef struct center {

  char center_name[100];

  char hostname[100];

  int port;

  struct center *next_center;

} center;

I need to add elements to the list. But these elements that i need to add are on a file, so:

 int main(int argc, char** argv) {
     center *head = NULL; 
     parse(argv, head);
  }

parse is a function, which read the file and add those read elements to a new center (all of this works, it double checked)

void parser (char** argv, center *head) {
   //read the elements i need to add
   //creates a newCenter and adds the elements read to the new center
   //this far it works
  addToCenter(newCenter, head); 
}

where:

addToCenter(center *newCenter, center *head){
   //adds newCenter to the list
   if (head == null)
      head = newCenter;
   else {
      //find last element
      lastelement.next_center = newCenter; 
   }

}

Everything works, except that the list on Main always return as null. In other words, the reference is not being modified. I dont understand why, because i am passing a pointer to the list.

another solution i though is to create the head variable of the list as a global variable, but its better to avoid those situations.

Thanks in advance.

  • 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-17T20:50:05+00:00Added an answer on June 17, 2026 at 8:50 pm

    Your list head is being passed by value. You need to pass the head pointer by address in case it is modified (which it will be).

    Example:

    addToCenter(center *newCenter, center *head) // <=== note: passed by value
    {
       //adds newCenter to the list
       if (head == null)
          head = newCenter; // <=== note: modified local stack parameter only.
       else {
          //find last element
          lastelement.next_center = newCenter; 
       }
    }
    

    should be:

    addToCenter(center *newCenter, center **head) // <=== note: now passed by address
    {
       //adds newCenter to the list
       if (*head == null)
          *head = newCenter;  // <=== note: now modifies the source pointer.
       else {
          //find last element
          lastelement.next_center = newCenter; 
       }
    }
    

    Likewise with parse:

    void parser (char** argv, center **head) // <=== again, the head-pointer's *address*
    {
       //read the elements i need to add
       //creates a newCenter and adds the elements read to the new center
       //this far it works
      addToCenter(newCenter, head);  // <=== just forward it on.
    }
    

    And finally back in main:

    int main(int argc, char** argv) 
    {
         center *head = NULL; 
         parse(argv, &head);  // <=== note: passing address of the head-pointer. (thus a dbl-pointer).
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list definition in my solution, and i want programmatically be able
I have a function definition in my VC++ Win32 DLL DEMO2_API void ProcessData(char* i_buff,
Which advantages have definition list ( <dd><dt> etc..) and when we should use it?
I have a list definition with a custom form to display all my list
Does definition list <dl> require that each <dd> will have <dt> tag? Example: option1
I have a rather small part of my webpage that uses a definition list
I have a definition list and I need to delete all the <dt> tags
I have a definition list and I would like the text description to stay
I have a Custom List Definition (schema.xml) i have set up Site Columns (through
Given following definition of global (or static local) variable: static const <type>* const ptr

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.