Linked list function

This is a discussion on Linked list function within the C Programming forums, part of the General Programming Boards category; Code: void traverse (link h, void (*visit)(link)) { if (h == NULL) { (*visit)(h); traverse(h->next, visit); } } What exactly ...

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Linked list function

    Code:
    void traverse (link h, void (*visit)(link)) {
      if (h == NULL) {
        (*visit)(h);
        traverse(h->next, visit);
      }
    }
    What exactly does that second parameter mean? I have never seen such an argument before.
    =========================================
    Everytime you segfault, you murder some part of the world

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    A pointer to a function.

    It might look like:

    Code:
    void myfunction(link x)
    {
        return;
    }
    
    /* ... */
    
    /* call traverse */ 
    traverse(h, &myfunction);
    
    /* ... */
    See http://www.newty.de/fpt/index.html
    Last edited by zacs7; 04-07-2008 at 05:19 AM.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I get your example, because i have used functions that way before.

    But I don't get what (*visit)(h) is. Does it mean the function visit is pointing to the h node
    =========================================
    Everytime you segfault, you murder some part of the world

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    One can also write:
    Code:
    traverse(h, myfunction);
    EDIT:
    But I don't get what (*visit)(h) is. Does it mean the function visit is pointing to the h node
    visit is a function pointer, so it is just calling the function and passing h as the argument. You can also write visit(h).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Calls what's pointed to by visit with the parameter h.

    No need to dereference, it's the same as
    Code:
    visit(h);
    Basically,
    Code:
    traverse(h, myfunction);        /* short way */
    traverse(h, &myfunction);       /* long way */
    Code:
    visit(h);                       /* short way */
    (*visit)(h);                    /* long way */
    Both are correct.
    Last edited by zacs7; 04-07-2008 at 05:38 AM.

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Thanks guys.

    My lecturer seems intent on confusing people to no end, but then again perhaps it's a good thing I learnt this style of coding. Pretty easy then.

    Thanks zacs7 and laserlight =)
    =========================================
    Everytime you segfault, you murder some part of the world

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Writing the form
    Code:
    (*visit)(h);
    makes sure that it's CLEAR that visit is a funciton pointer - it's not needed, and I don't use that form when I use function pointers - but it is valid, and offers a slightly higher degree of "explaining what is going on" (assuming of course you can follow it).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    makes sure that it's CLEAR that visit is a funciton pointer - it's not needed, and I don't use that form when I use function pointers - but it is valid, and offers a slightly higher degree of "explaining what is going on" (assuming of course you can follow it).
    The trade off of this slightly higher degree of explanation is that it delves into the details. I think that taking a typedef and then using "normal" function call syntax makes for a higher level of abstraction instead, e.g.,
    Code:
    typedef void (*VisitFunction)(link);
    
    void traverse(link h, VisitFunction visit) {
      if (h == NULL) {
        visit(h);
        traverse(h->next, visit);
      }
    }
    Incidentally, h == NULL looks like it should be h != NULL instead since h->next would result in the dereferencing of a null pointer if h == NULL.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Replies: 5
    Last Post: 11-04-2006, 05:39 PM
  5. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21