C passing a list as an argument

So, I have a list definition 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 items to the list. But these elements that I need to add are in the file, therefore:

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

parse is a function that reads a file and adds these reading elements to a new center (all this works, it is 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 Main list is always returned as null. In other words, the link does not change. I do not understand why, because I pass a pointer to a list.

another solution, although I have to create the main list variable as a global variable, but it is better to avoid such situations.

Thanks in advance.

+5
source share
5 answers

. , ( ).

:

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; 
   }
}

:

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; 
   }
}

:

void parser (char** argv, center **head) // <=== again, the head-pointer *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.
}

, , :

int main(int argc, char** argv) 
{
     center *head = NULL; 
     parse(argv, &head);  // <=== note: passing address of the head-pointer. (thus a dbl-pointer).
}
+6

:

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); 
}

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

}

:

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

, C . , . head , , . , head (, NULL)

+3

. - .

head parser addToCenter, head .

void parser(char** argv, centerPtr **headPtr) {
     /* rewrite using *headPtr instead of head */
}
void addToCenter(center *newCenter, center **headPtr) {
     /* rewrite using *headPtr instead of head */
}
int main(int argc, char** argv) {
    ....
    parser(argv, &head);
    ....
}
+2

In your functions, you change the local copy of the pointer, not the pointer itself. You must change the definitions of your functions to make a pointer to a pointer.

+1
source

It seems that you are missing some of the knowledge about pointer logic. I will try to describe what is wrong here.

First of all, rewrite any void*as intthat is (sort of) what happens inside.

addToCenter(int newCenter, int head){
   //adds newCenter to the list
   if (head == 0)
       head = newCenter;

And there you go, the problem illustrates itself.

Decision?

addToCenter(center *newCenter, center **head){
   //adds newCenter to the list
   if (*head == null)
       *head = newCenter;
+1
source

All Articles