Thread: Shifting pointers in a function

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Shifting pointers in a function

    Hello!

    I want to shift elements in a linked list by one element. If I do it directly in the function, where I need it, it is of no problem to me. But I come up with problems, as soon as I want to do the shifting of pointers in an own function to keep this more generic.

    See this code:

    Code:
    // linked list of n elements present, allocated somewhere else
    
    void shift_to_next( struct test *file, struct test *start_of_list_file ) {
    	if( file->next == NULL ) file = start_of_list_file;
    	else file = file->next;
    }
    
    int main(void) {
    //	*start and *tmp are allocated somewhere else, the linked list can be seen as established, this would just blow up this question too much. this topic is not about crating a linked list... ;)
    
    	tmp = start;
    	shift_to_next( tmp, start );
    	// at this point, tmp should point to the second element in the list
    }
    The pointers in the function are shifted correctly, but the pointer out of the function - in the main() program - are not shifted, they stay the same.

    What would I have to do in order to get the pointers in the main-program affected?

    Kind regards,
    Herakles
    Last edited by herakles; 03-18-2011 at 09:21 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You just need to pass in a pointer to the pointer:
    Code:
    void shift_to_next( struct test **file, struct test *start_of_list_file )
    {
      if( (*file)->next == NULL ) *file = start_of_list_file;
      else *file = (*file)->next;
    }
    And then call the function like this:
    Code:
    shift_to_next( &tmp, start );
    Of course, another way would be to just make shift_to_next() return the new pointer. Then you could just have this:
    Code:
    struct test *shift_to_next( struct test **file, struct test *start_of_list_file )
    {
      return file->next == NULL ? start_of_list_file : file->next;
    }
    Code:
    tmp = shift_to_next( tmp, start );
    Last edited by itsme86; 03-18-2011 at 09:38 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class function pointers
    By VirtualAce in forum C++ Programming
    Replies: 40
    Last Post: 02-17-2005, 12:55 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 04:22 PM