www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C Function Parameters

reply Tyro <ridimz yahoo.com> writes:
Could someone please tell me if I'm reading the following correctly

	int myfunc(MyStruct *ms, void * (*allocF)(size_t s))

The second parameter above [void * (*allocF)(size_t s)], is a pointer to 
a function that accepts a size_t parameter and returns a void pointer? 
If so, I have two questions:

	1) Why is the function  pointed to being dereferenced? Why [(*allocF)] 
instead of [(allocF)]?

	2) Is [void function(size_t s) allocF] a suitable replacement or would 
it be better to use a delegate?

Thanks
-- 
Andrew C. Edwards
-----------------------------------------------------
The truth we call D, has passed through three stages:
   First ridiculed; Then violently opposed;
   And now, it is being accepted as self-evident.
Consequently:
   C/C++ is rapidly approaching Stage 5 (being forgotten)!
Sep 05 2006
next sibling parent xs0 <xs0 xs0.com> writes:
Tyro wrote:
 Could someone please tell me if I'm reading the following correctly
 
     int myfunc(MyStruct *ms, void * (*allocF)(size_t s))
 
 The second parameter above [void * (*allocF)(size_t s)], is a pointer to 
 a function that accepts a size_t parameter and returns a void pointer? 
 If so, I have two questions:
 
     1) Why is the function  pointed to being dereferenced? Why 
 [(*allocF)] instead of [(allocF)]?

It's not being dereferenced, the * just means allocF is a pointer (to a function). It's in parentheses because it would otherwise get parsed as a function returning void** (as opposed to a function _pointer_ returning void*), which is illegal as a parameter, afaik.
     2) Is [void function(size_t s) allocF] a suitable replacement or 
 would it be better to use a delegate?

Depends :) If myfunc is a C function, you have to use a function; if it's a D function, it's usually better to use a delegate.. xs0
Sep 05 2006
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Tyro wrote:
 Could someone please tell me if I'm reading the following correctly
 
     int myfunc(MyStruct *ms, void * (*allocF)(size_t s))
 
 The second parameter above [void * (*allocF)(size_t s)], is a pointer to 
 a function that accepts a size_t parameter and returns a void pointer? 

Yes. <snip>
     2) Is [void function(size_t s) allocF] a suitable replacement

No. However, this is: int myfunc(MyStruct* ms, void* function(size_t s) allocF) I always use the D function/delegate notation. Much easier to read than the C notation. Note also that the '*' has been attached to the type, which is more logical in D.
 or would it be better to use a delegate?

Whether you should use function or delegate depends on what you're going to pass in. function - module level functions or static member functions delegate - member functions acting on a specific object, or nested functions Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Sep 05 2006