Thread overview
function poiners
Feb 06, 2004
imr1984
Feb 06, 2004
Manfred Nowak
Feb 06, 2004
Andy Friesen
Feb 06, 2004
Ilya Minkov
Feb 07, 2004
Sean Kelly
Feb 08, 2004
Walter
February 06, 2004
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.


February 06, 2004
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.
February 06, 2004
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
February 06, 2004
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

February 07, 2004
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

February 08, 2004
"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,
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.

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