Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: pointers to functions Message-ID: <19500@mimsy.UUCP> Date: 9 Sep 89 20:46:18 GMT References: <1679@hydra.gatech.EDU> <9429@chinet.chi.il.us> <22005@cup.portal.com> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 70 [given the declaration `int (*f)();'] In article <22005@cup.portal.com> ts@cup.portal.com (Tim W Smith) writes: >I'm not sure, but I think this allowing of f() came in with PCC. It was >clearly a bug, which the ANSI committee has chosen to make a feature. It is not so clear, particularly when done in in C++-style: object->manipuator_function(object); Here it is a nice notation and seems deserving of consideration. If the notation does not otherwise muck up the abstraction of the language, i.e., if it is `free', why not allow it? The question then is whether it mucks up the abstraction. Read on: >While we are on the subject, try this one: > (***************f)(); >This will also invoke the function on most compilers. Tell me with a >straight face that this is not a bug. Okay: This is not a bug. >Does ANSI require this to work? Yes. The whole thing falls rather naturally out of an alternative view of all C function calls, in which one decides that `functions' do not really exist. Rather like arrays, one can *declare* an object as a function, but in most contexts, mentioning the *name* of the function gives instead a pointer to that function. We already knew the latter, from K&R: int foo(); /* declare foo as function */ int (*f)() = foo; /* name of function => ptr to function */ The `alternative interpretation' above holds that this is actually true elsewhere as well: that when we write int foo(); foo(3); what we really wrote was int foo(); foo /* this is the name of a function, so it becomes a pointer to the function */ ( /* this means `apply the pointer on the left */ 3 /* argument to the function */ ) /* end of the argument list */ ; In this view, when f is an object of type `pointer to function of args returning T', the result of the indirection `*f' is the `name' of a function, which is immediately converted back to a pointer. Hence `**f' says `Indirect, obtaining the ``name'' of the function, and then immediately revert to the pointer; then indirect again, and immediately revert again.' No matter how many `*' characters you add, the compiler simply keeps reverting your function to a pointer-to-function. The real secret is the decision that the `function locator' that goes before the left parenthesis that introduces the arguments (if any) is an ordinary rvalue, and that functions in rvalue contexts become pointers to functions (just like arrays become pointers to their first members). -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris