Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I edit a pointer to a list node from a function in a recursion?

I have been writing a program that is quite complex compared to what I have dealt with until now. Anyways at some point I am supposed to write a function that will manipulate a struct list. I'm trying to make this question as simple as possible so I wrote down a pretty simple piece of code just for reference.

Here is the thing: at first I call testf from another function providing it with a valid current as well as an i with a value of 0. This means that testf will call itself about 100 times before it starts accessing the rest of the code. This is when all the generated instances of testf will start getting resolved.

 void testf(listnode *current, int *i) {
   wordwagon *current2;

   current2 = current;
   if (*i < 100) {
     *i = *i + 1;
     current2 = current2->next;
     testf(current2, i);
   }


   current = current->next;
   return;
 }

If, let's say, I have enough connected list nodes at my disposal, is current = current->next; the correct way for the "last" testf function to access and edit the caller's current2 value (which is this function's current), or am I horribly wrong? If I am, what is the way to make changes to the caller function's variables from inside the called function and be sure they won't go away as soon as the called function returns? I find it kind of hard to get a good grasp on how pointers work.

It is very likely that I have left out important information or that I haven't asked my question clearly enough. Please inform me if that is the case so I can edit in whatever you need.

Thanks in advance.

like image 232
Eternal_Light Avatar asked Jan 03 '12 05:01

Eternal_Light


People also ask

How to change head pointer in linked list?

2) Functions that modify the head pointer: The algorithm to solve the problem is a simple 3 step process: (a) Store the head pointer (b) change the head pointer to point to the next node (c) delete the previous head node.

How do you pass the head of a linked list to a function in C?

Your function insert gets the pointers by value, so when it modifies head, it modifies the local copy of the pointer. insert does not change the head variable you defined in main . void insert(snode **head, snode **last); insert(&head, &last);

What is the meaning of node * &head?

However, Node* &head is a reference to a pointer to a node. So basically it passes the pointer to the the head by reference, so you can change the value of the head ptr.


1 Answers

You can pass pointer to a pointer in your function, and derefrence it to get a listnode pointer back , here is how the code will look like after that ( not tested for compilation ) :

void testf(listnode **current, int *i) {  // accept pointer to listnode pointer
   wordwagon *current2;

   current2 = *current;   // retreive pointer value by dereferece
   if (*i < 100) {
     *i = *i + 1;
     current2 = current2->next;
     testf(&current2, i);  // recursively call by reference to the pointer
   }

   *current = (*current)->next; /* change the current pointer next pointer, CORRECTED as suggested by Azure */
   return;
 }

Here is a list of really good articles for learning pointers :

a) http://cslibrary.stanford.edu/102/PointersAndMemory.pdf

b) http://cslibrary.stanford.edu/103/LinkedListBasics.pdf

like image 132
DhruvPathak Avatar answered Nov 09 '22 21:11

DhruvPathak