Thread: question on syntax

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

    question on syntax

    Code:
    void search_list(datalist_t *list, int key,
       void (*print_data_func)(void*))
    {
    
        	datalist_t *p;
    
     	p = list;
    	while (p != NULL ) {
    
    		if  (p->item_key == key ) {
    		print_data_func(p->item_value);
    		break;
    	}
    
    	p = p->next;
    
    }
    
    }
    Can anyone help me understand the second line? Is this a pointer to a pointer or something? And whats with the voids?

    thanks

  2. #2
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by nasser View Post
    Code:
    void search_list(datalist_t *list, int key,
       void (*print_data_func)(void*))
    {
    
        	datalist_t *p;
    
     	p = list;
    	while (p != NULL ) {
    
    		if  (p->item_key == key ) {
    		print_data_func(p->item_value);
    		break;
    	}
    
    	p = p->next;
    
    }
    
    }
    Can anyone help me understand the second line? Is this a pointer to a pointer or something? And whats with the voids?

    thanks
    if I'm not mistaken, that's a pointer to a function that takes a pointer to type void as a parameter and returns nothing (void).
    Do not stare at my avatar.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    I see, but why would you need a pointer to a function? :s

    Couldn't you just create a function prototype for print_data_func and call it like normal??

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The thing on the second line is indeed called a function pointer.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    thanks for the link...looks like theres plenty to learn :/

  6. #6
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by nasser View Post
    I see, but why would you need a pointer to a function? :s

    Couldn't you just create a function prototype for print_data_func and call it like normal??
    well, sometimes some people like to do things their own way, hence the function pointer
    Last edited by cph; 10-18-2011 at 08:10 PM.
    Do not stare at my avatar.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Syntax question
    By dotunix in forum C Programming
    Replies: 4
    Last Post: 12-31-2008, 05:59 AM
  2. Syntax question
    By Stonehambey in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2008, 06:11 AM
  3. syntax question
    By 182 in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2006, 02:36 PM
  4. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  5. syntax question
    By InvariantLoop in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2004, 07:58 AM