home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / std / c / 2356 < prev    next >
Encoding:
Text File  |  1992-07-21  |  3.1 KB  |  78 lines

  1. Path: sparky!uunet!mcsun!uknet!warwick!rlinf!inf.rl.ac.uk
  2. From: jjsc@inf.rl.ac.uk (John Cullen)
  3. Newsgroups: comp.std.c
  4. Subject: Re: dereferencing function pointer as an expression
  5. Keywords: functions pointers dereferencing
  6. Message-ID: <12632@pecan.rl.ac.uk>
  7. Date: 22 Jul 92 08:19:50 GMT
  8. Sender: jjsc@inf.rl.ac.uk
  9. Reply-To: GORMAN_B@P1.LANCSP.AC.UK (Barry Gorman)
  10. Organization: Rutherford Appleton Laboratory, Informatics Department
  11. Lines: 65
  12.  
  13. This article is being posted on behalf of a friend without direct access to
  14. netnews. Please reply to him direct and/or followup to this group.
  15.  
  16. John Cullen
  17.  
  18. ----------------------------Cut Here------------------------------
  19.  
  20. From: Barry Gorman  <GORMAN_B@uk.ac.LANCSP.P1>
  21. Date:         Tue, 21 Jul 92 13:57:25
  22. Subject:      Re: dereferencing function pointer as an expression
  23.  
  24. In article 6839 Norman Diamond writes:
  25. > In article .. Mike Lijewski writes:
  26. >>int f(int i) { return i + 1;}
  27. >>main(){
  28. >>       int (*fp)(int) = f;
  29. >>       if (*f) puts("isn't life fun");}
  30. >>}
  31. > .... the if always succeeds.
  32.  
  33. Now I saw the original article, and thought that the test should have
  34. been "if (*fp) ...", which I thought I understood; but the "if (*f) ..."
  35. threw me into confusion. This was resolved by reading the ANSI Standard.
  36. -----
  37. 3.2.2.1 Lvalues and Function Designators (line 20)
  38.  
  39. Except when it is the operand of a sizeof operator or the unary & operator,
  40. a function designator with type "function returning type" is converted to
  41. an expression that has type "pointer to function returning type".
  42. -----
  43. 3.3.2.2 Function Calls (line 18)
  44.  
  45. The expression that denotes the called function shall have type pointer to
  46. function returning void or returning an object type other than array type.
  47. -----
  48. I must confess that this was news to me. It seems that a simple function call
  49. such as " y = sin(x); " is more complex than I thought. The "sin" is initally
  50. converted to a pointer to conform to #3.2.2.1 and the call requires a pointer
  51. to conform to #3.3.2.1, however these effectively cancel, and I can sleep at
  52. nights again, or can I ?
  53.  
  54. What about the " if (*f)... ", what does the standard say:
  55. -----
  56. 3.3.3.2 Address and Indirection Operators (line 43)
  57.  
  58. The unary * operator denotes indirection. If the operand points to a function,
  59. the result is a function designator; ...
  60. -----
  61. All is now clear. First of all, the "f" is converted to a pointer according
  62. to #3.2.2.1. Then the "*f" is converted back to a function designator according
  63. to #3.3.3.2. Finally the expression "*f" is converted back again to a pointer,
  64. which is then (implicitly) compared to NULL (as the function is not called).
  65. Had it been (if ((*f)(2)) ...) then #3.3.2.2 applies, and all would be well.
  66.  
  67. Many thanks to the ANSI Committee who produced such a readable and well
  68. indexed Standard. It took me only a few moments to find the above information.
  69. May I recommend that you all purchase the Standard, or pester your librarian
  70. to obtain a copy (as I did).
  71.  
  72. In view of the discussion of "F" in comp.lang.c, may I suggest that the
  73. appropriate word is contained in the statement: "Read The FINE Manual".
  74.  
  75. Barry
  76. .....
  77. Lancashire Polytechnic is now known as The University of Central Lancashire.
  78.