www.digitalmars.com         C & C++   DMDScript  

D - function poiners

reply imr1984 <imr1984_member pathlink.com> writes:
why is it that when you want to assign a function pointer to a function, you
have to use the address of operator ( & ) unlike in C/C++. This kinda annoys me
because im at university where they force me to use C, and im always having to
think to myself "should i put an ampersand here or not?". At the very least make
it optional.
Feb 06 2004
next sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
On Fri, 06 Feb 2004 13:12:00 +0000, imr1984 wrote:

 why is it that when you want to assign a function pointer to a function, you
 have to use the address of operator ( & ) unlike in C/C++.

Have you read http://www.digitalmars.com/d/property.html, [cited 06.02.04] about functions as properties? And, I think I do not grep you fully: do you really mean assigning a function pointer to a function, or the other way round? So long.
Feb 06 2004
prev sibling next sibling parent Andy Friesen <andy ikagames.com> writes:
imr1984 wrote:

 why is it that when you want to assign a function pointer to a function, you
 have to use the address of operator ( & ) unlike in C/C++. This kinda annoys me
 because im at university where they force me to use C, and im always having to
 think to myself "should i put an ampersand here or not?". At the very least
make
 it optional.
 

The rule is really simple in D: You *always* need the ampersand when taking the address of something. -- andy
Feb 06 2004
prev sibling next sibling parent Ilya Minkov <minkov cs.tum.edu> writes:
imr1984 wrote:
 why is it that when you want to assign a function pointer to a function, you
 have to use the address of operator ( & ) unlike in C/C++. This kinda annoys me
 because im at university where they force me to use C, and im always having to
 think to myself "should i put an ampersand here or not?". At the very least
make
 it optional.

Don't gesitate to put an ampersand (&) in C. It works just as well. I find that it's a point of inconsistency in C that you can use both adress-of and a function name itself as if it was the same. If you put & in C you don't get adress-of-adress, it's simply the same as without in context of function pointers. You have to put yet another & to get adress-of-adress IIRC. (btw, don't do this adress of pointer trick unless you know where this adress is stored! or don't do it at all!). Finally, both C++ and D have a more elegant replacement to function pointers, which is virtual methods of classes. In C i got caught a few times. I forgot to put () after a function call without parameters. And since this yuilds a function adress, which is a value, which is discarded since nothing sensible is done with it, the function doesn't get called. Nothing happens. The program just runs further. I tried to trace into the function with a debugger for hours, till i finally saw that i left out ()! :> So i guess it would be good that it stays an error to assign a function to function pointer. It happened a few times while writing my first C prog - i was a more or less experienced Delphi developer back then and was really frustrated by endless bugs caused by C - and then when using BCX which generated wrong code of this sort under some circumstance. -eye PS. tomorrow i have an exam - wish me luck
Feb 06 2004
prev sibling next sibling parent Sean Kelly <sean ffwd.cx> writes:
imr1984 wrote:

 why is it that when you want to assign a function pointer to a function, you
 have to use the address of operator ( & ) unlike in C/C++. This kinda annoys me
 because im at university where they force me to use C, and im always having to
 think to myself "should i put an ampersand here or not?". At the very least
make
 it optional.

In C/C++ the address-of operator is implied for C-style functions so use of it is optional. It's not implied for member function pointers so use there is mandatory AFAIK. I like that in D they enforce a degree of consistency here. But that also means you're free to always use the '&' in C as well. Sean
Feb 07 2004
prev sibling parent "Walter" <walter digitalmars.com> writes:
"imr1984" <imr1984_member pathlink.com> wrote in message
news:c003r0$l5v$1 digitaldaemon.com...
 why is it that when you want to assign a function pointer to a function,

 have to use the address of operator ( & ) unlike in C/C++. This kinda

 because im at university where they force me to use C, and im always

 think to myself "should i put an ampersand here or not?". At the very

 it optional.

It's mandatory in D to eliminate parsing ambiguities with respect to properties.
Feb 07 2004