Article View: comp.std.c
Article #110Re: dereferencing function pointer as an expression
From: John Cullen
Date: Wed, 22 Jul 1992 08:19
Date: Wed, 22 Jul 1992 08:19
65 lines
3272 bytes
3272 bytes
This article is being posted on behalf of a friend without direct access to
netnews. Please reply to him direct and/or followup to this group.
John Cullen
----------------------------Cut Here------------------------------
From: Barry Gorman <GORMAN_B@uk.ac.LANCSP.P1>
Date: Tue, 21 Jul 92 13:57:25
Subject: Re: dereferencing function pointer as an expression
In article 6839 Norman Diamond writes:
> In article .. Mike Lijewski writes:
>>int f(int i) { return i + 1;}
>>main(){
>> int (*fp)(int) = f;
>> if (*f) puts("isn't life fun");}
>>}
> .... the if always succeeds.
Now I saw the original article, and thought that the test should have
been "if (*fp) ...", which I thought I understood; but the "if (*f) ..."
threw me into confusion. This was resolved by reading the ANSI Standard.
-----
3.2.2.1 Lvalues and Function Designators (line 20)
Except when it is the operand of a sizeof operator or the unary & operator,
a function designator with type "function returning type" is converted to
an expression that has type "pointer to function returning type".
-----
3.3.2.2 Function Calls (line 18)
The expression that denotes the called function shall have type pointer to
function returning void or returning an object type other than array type.
-----
I must confess that this was news to me. It seems that a simple function call
such as " y = sin(x); " is more complex than I thought. The "sin" is initally
converted to a pointer to conform to #3.2.2.1 and the call requires a pointer
to conform to #3.3.2.1, however these effectively cancel, and I can sleep at
nights again, or can I ?
What about the " if (*f)... ", what does the standard say:
-----
3.3.3.2 Address and Indirection Operators (line 43)
The unary * operator denotes indirection. If the operand points to a function,
the result is a function designator; ...
-----
All is now clear. First of all, the "f" is converted to a pointer according
to #3.2.2.1. Then the "*f" is converted back to a function designator according
to #3.3.3.2. Finally the expression "*f" is converted back again to a pointer,
which is then (implicitly) compared to NULL (as the function is not called).
Had it been (if ((*f)(2)) ...) then #3.3.2.2 applies, and all would be well.
Many thanks to the ANSI Committee who produced such a readable and well
indexed Standard. It took me only a few moments to find the above information.
May I recommend that you all purchase the Standard, or pester your librarian
to obtain a copy (as I did).
In view of the discussion of "F" in comp.lang.c, may I suggest that the
appropriate word is contained in the statement: "Read The FINE Manual".
Barry
.....
Lancashire Polytechnic is now known as The University of Central Lancashire.
Technical Details
Message-ID:
<12632@pecan.rl.ac.uk>Path:
arc1-ca.pugleaf.net!sparky!uunet!mcsun!uknet!warwick!rlinf!inf.rl.ac.ukShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!uknet!warwick!rlinf!inf.rl.ac.uk From: jjsc@inf.rl.ac.uk (John Cullen) Newsgroups: comp.std.c Subject: Re: dereferencing function pointer as an expression Keywords: functions pointers dereferencing Message-ID: <12632@pecan.rl.ac.uk> Date: 22 Jul 92 08:19:50 GMT Sender: jjsc@inf.rl.ac.uk Reply-To: GORMAN_B@P1.LANCSP.AC.UK (Barry Gorman) Organization: Rutherford Appleton Laboratory, Informatics Department Lines: 65