Microsoft KB Archive/32109

= Descriptions of two syntaxes for calling functions by using pointers =

Article ID: 32109

Article Last Modified on 4/24/2006

-

APPLIES TO


 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition

-



This article was previously published under Q32109





Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.



SUMMARY
The source code below contains what appears to be an improper use of a pointer to a function. However, the code is correct. There are two ways to call a function when using a pointer to a function: (*pointer_to_function); pointer_to_function; The behavior exhibited in the sample code is expected. The ANSI Standard (Document Number X3J11/88-002, January 11, 1988) allows a function to be called through a pointer with the following syntax (*pointer_to_function); in addition to the following nontraditional syntax: pointer_to_function; The text below is quoted from page 41 of the &quot;Rationale for Draft Proposed American National Standard for Information Systems Programming Language C&quot; (sec. 3.3.2.2, &quot;Function calls&quot;):

The latter construct, not sanctioned in the Base Document, appears in some present versions of C, is unambiguous, invalidates no old code, and can be an important shorthand.



MORE INFORMATION
The sample code below demonstrates this method.

Sample code
/* * Compile options needed: none */


 * 1) include 

void main(void) {  void ftn(void); void (*ptr_to_ftn)(void);

ptr_to_ftn = ftn;  // The pointer is correctly assigned // the address of 'ftn'

printf(&quot;\nCalling the function\n\n&quot;);

ptr_to_ftn;    // This is not traditional syntax for // a call through a function pointer

printf(&quot;back to main\n&quot;); }

void ftn(void) {  printf(&quot;in the function\n\n&quot;); }

Keywords: kbinfo kblangc KB32109

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.