What exactly does that second parameter mean? I have never seen such an argument before.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.Code:void traverse (link h, void (*visit)(link)) { if (h == NULL) { (*visit)(h); traverse(h->next, visit); } }
=========================================
Everytime you segfault, you murder some part of the world
A pointer to a function.
It might look like:
See http://www.newty.de/fpt/index.htmlCode:void myfunction(link x) { return; } /* ... */ /* call traverse */ traverse(h, &myfunction); /* ... */
Last edited by zacs7; 04-07-2008 at 05:19 AM.
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
One can also write:
EDIT:Code:traverse(h, myfunction);
visit is a function pointer, so it is just calling the function and passing h as the argument. You can also write visit(h).But I don't get what (*visit)(h) is. Does it mean the function visit is pointing to the h node
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Calls what's pointed to by visit with the parameter h.
No need to dereference, it's the same as
Basically,Code:visit(h);
Code:traverse(h, myfunction); /* short way */ traverse(h, &myfunction); /* long way */Both are correct.Code:visit(h); /* short way */ (*visit)(h); /* long way */
Last edited by zacs7; 04-07-2008 at 05:38 AM.
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
Writing the formmakes 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).Code:(*visit)(h);
--
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.
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.,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).
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.Code:typedef void (*VisitFunction)(link); void traverse(link h, VisitFunction visit) { if (h == NULL) { visit(h); traverse(h->next, visit); } }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)