views:

116

answers:

3

BIG EDIT:

Ok, my original question didn't help me. Here is a second go.

My struct looks like this:

struct node {
   char *name;
   int  age;
   struct node *nextName;
   struct node *nextAge;
}; 

I have to make two linked lists out of structures like this,. So i have 'rootAges' which keeps track of where the Age-based list starts and 'rootNames' which keeps track of where the names start. I can't seem to get these to update.

that is, i have struct node *rootAges and struct node *rootNames. I need to pass both of these to a function which adds the elements to the list. But i also need the roots to change as I add things to the list. the methods provided so far, haven't changed the value of rootAges for example in the main function, when it is altered in the add function.

Thanks!

+5  A: 

You pass the address of a structure's instance to a function that accepts a pointer in C.

void fn(struct data *p)
{
  if(p)
    p->x = 33;
} 

//... main ...
struct data d;
fn(&d);
//d.x == 33
Brian R. Bondy
+1  A: 

Pass a pointer to the structure.

For example:

typedef struct data{ int x;} s_data;

void foo (s_data* pointer){}

s_data s={0};
foo(&s);
Tom
A: 

Declaration

void Foo(struct data *);

Definition

void Foo(struct data *p)
{
//body
}

In your code

Foo(root);
mihirpmehta