INF: Two Syntaxes for Calling Functions with Pointers

ID Number: Q32109

5.10 6.00 6.00a 6.00ax 7.00

MS-DOS

Summary:

The source code below contains what appears to be an improper use of a

pointer to a function. However, the compiler fails to flag this either

as a warning or as an error, and the code seems to work as expected.

This information applies to Microsoft C versions 5.1, 6.0, 6.0a,

6.0ax, and C/C++ version 7.0 for MS-DOS systems.

More Information:

The behavior exhibited in the sample code is expected. The proposed

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 non-traditional syntax:

pointer_to_function();

The following is a quotation from page 41 of "Rationale for Draft

Proposed American National Standard for Information Systems

Programming Language C" (sec. 3.3.2.2, "Function calls"):

"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."

The sample code below demonstrates this method.

Sample Code

-----------

/* Compile options needed: none

*/

#include <stdio.h>

void main()

{

void ftn();

void (*ptr_to_ftn)();

ptr_to_ftn = ftn; /* The pointer is correctly assigned */

/* the address of 'ftn()'. */

printf("\nCalling all ftns\n\n");

(ptr_to_ftn)(); /* This is not traditional syntax for */

/* a call via a function pointer */

printf("back to main\n");

}

void ftn()

{

printf("inside ftnland\n\n");

}

Additional reference words: 6.0 6.00 6.0a 6.00a 6.0ax 6.00ax 7.00