1

In C, I want to pass a pointer to another function so I coded the following,

node* list // contains linked list of nodes

void function (node* list){

/*Whatever I do here, I want to make sure that I modify the original list, 
  not the local copy. +

  how do I pass the original pointer 'list' to this function so that I'm working  
  on the same variable? */

}

[EDIT]

I just like to clarify few things here,

I actually have a void function which takes a struct argument,

void function (struct value arg){ }

and in the struct, I'm defining one of my internal variables as,

node* list-> list;

so in my function, if I do something like,

arg->list

am I accessing the original list variable?

lukieleetronic
  • 623
  • 7
  • 10
  • 23
  • possible duplicate of [What are the barriers to understanding pointers and what can be done to overcome them?](http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome) – Alex Celeste Jun 04 '15 at 15:54
  • do you intend to delete elements in that function ? – Pierre Lacave Jun 04 '15 at 15:55
  • Does your function take a pointer or a structure? You've shown both. – Keith Thompson Jun 04 '15 at 15:58
  • `node* list // contains linked list of nodes` -- No, it doesn't *contain* a linked list. `list` is a pointer to a `node`. Presumably a `node` is a single node of a linked list. – Keith Thompson Jun 04 '15 at 15:58

1 Answers1

1

To answer your edit:

No, you are passing the structure by value, so the function contains a copy of the structure. If you don't return the structure in the function and grab it from where you called the function from, it will be lost.

Keep in mind that C does not pass by reference. It is instead simulated by passing the pointer. You could instead do this:

void function (struct value* arg)
{
....
}

And it will modify the original structure.

Lawrence Aiello
  • 4,560
  • 5
  • 21
  • 35