Thread: static pointer in a recursive function?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Location
    London
    Posts
    4

    static pointer in a recursive function?

    I am new to C and I'm trying to learn more about pointers.

    I have a working function that reverses a linked list recursively.

    My working function looks like this:
    Code:
    /******************************************
     * Parameters: 
     * 	- Reference pointer to head of list
     * 	- Pointer to first node in list
     ******************************************/
    void recursive_reverse(struct node **head_ptr, struct node *first)
    {
    	struct node *dummy;
    	
    	if(first->next == NULL)
    		return;
    
    	dummy = first->next;
    	first->next = dummy->next;
    	dummy->next = *head_ptr;
    	*head_ptr = dummy;
    	recursive_reverse(head_ptr, first);
    }
    node is
    Code:
    struct node
    {
    	int data;
    	struct node *next;
    };
    My question is whether I can have a static pointer "first" (instead of the parameter "first") that gets initialised only once on the first itteration of the function and keeps track of where my original first node is?

    When I try:
    Code:
    void recursive_reverse2(struct node **head_ptr)
    {
    	static struct node *first = *head_ptr;
    	struct node *dummy;
    	
    	if(first->next == NULL)
    		return;
    
    	dummy = first->next;
    	first->next = dummy->next;
    	dummy->next = *head_ptr;
    	*head_ptr = dummy;
    	recursive_reverse2(head_ptr);
    }
    I get the compiler error:
    error: initializer element is not constant
    I don't know enough about the language to understand why I can't do this and would really appreciate any guidance.

    I'm sure there are other better ways to solve this specific problem but I would like to know if it's possible to adapt my solution with some kind of static pointer or if I'm wasting my time trying this route.

    many thanks

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You can't initialise a static declaration with a variable, the idea of static is that its initial value is known at compile time. A workaround to this would be to assign it to NULL, then check if it's NULL and assign it to *head_ptr:
    Code:
    static struct node *first = 0;
    if (!first)
        first = *head_ptr;
    Last edited by cwr; 09-19-2005 at 03:26 AM.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Location
    London
    Posts
    4
    Thank you very much for your explanation and workaround.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM