1

Im a little bit confused as to how I would go about passing references to double pointers, and I'm not even sure if this is what I'm looking for exactly.

Basically the issue I am running across is using doubly sorted linked lists. I have a function that takes in a pointer to my songNode to rearrange the songs in order of their rating. If a rating receives a zero it goes through this function to make sure the user provides a valid rating between 1 and 5 and reinserts it into the linked list.

The part I'm a little confused about is changing the initial value of the head that is passed into the first function like so:

songNode* head = NULL;
songNode* tail = NULL;

displaySongs(head);
printf("\n\n");
reassignSongs(head, tail);

The reassignSongs function takes a pointer to these song nodes.. And within the reassignSongs function it calls a function which takes a double pointer to these head and tail variables.

addSongToList(artist, title, rating, &head, &tail);
int addSongToList(char* artist, char* title, int rating, songNode** head, songNode** tail)

This function manipulates the head pointer, and when I get back to the main function the head variable is still how it is, because the reference to this variable wasn't changed..

I really just want to know how this is typically handled? Do you go into triple pointers? Quadruple pointers? Taking the address of a pointer?

Kyle Jensen
  • 419
  • 9
  • 27
  • Are you looking for pointers to pointers? `reassignSongs(&head, &tail);` with the definition `void reassignSongs (songNode **head, songNode **tail);`. – AntonH Jan 26 '16 at 01:55
  • Is that the way I should be doing it? My only question is when I do that how do I perform my other function calls? Do I pass in the address of the dereferenced double pointer? That looks weird to me I'm not sure if this is the way to go – Kyle Jensen Jan 26 '16 at 02:01
  • If you're looking to swap the pointers `head` and `tail` via a function, then I would do it with a function to which you pass the address of the pointers, making them pointers to pointers. [This SO question](http://stackoverflow.com/questions/897366/how-do-pointer-to-pointers-work-in-c) has an explaination on how to use them. – AntonH Jan 26 '16 at 02:05
  • Im not really swapping them, basicallyy what is happening is when I get back to my main function to display the songs in the correct order, the head variable hasn't been changed and displays the wrong information. – Kyle Jensen Jan 26 '16 at 02:31

1 Answers1

1

the definition of the reassignSongs function should look like:

void reassignSongs(songNode **head, songNode **tail) and your call would look like:

reassignSongs(&head, &tail);

The reason this is used is to change the address stored in either head or tail. To refer to the songNode which was pointed to by head in the calling function you must use (*head) or (*tail) to refer to the pointers to the nodes so that you may edit the head or tail of your list within the function.

  • So when I am within the reassignSongs function, I call another function that takes in a songNode** as well. So when passing those parameters to THAT function would I have to pass in &*head? – Kyle Jensen Jan 26 '16 at 02:42
  • @Kyle Jensen Sorry I'm late on the response but yes you would do this as long as you would like to change the actual value of the pointer, however, if you are trying to change values **stored** in the `struct` then you would not need the function to take `songNode **` but just `songNode *`. – Sanjay Charran Jan 26 '16 at 19:52
  • @Kyle Jensen If you do want to the function to receive `songNode **` and the type of `head` is already `songNode **` then you would, in the function call, simply pass the `head`. NOTE: `&*head` means to refer to the what is pointed to by `head` then `&` says to refer to the memory address of this so `&*head` has the same meaning as `head` so in a sense you are right in what must be passed. – Sanjay Charran Jan 26 '16 at 19:56