INF: Declaring a Pointer to a Function: C4071

ID Number: Q49064

5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

SYMPTOMS

In Microsoft C versions 5.1, 6.0, 6.0a, 6.0ax, and C/C++ version 7.0

the old method of declaring a pointer to a function,

type (*ptr)();

ptr = function;

causes the following compiler warnings when compiling with the

"/W3" option in C versions 6.0, 6.0a, and 6.0ax:

C4071: 'ptr' : no function prototype given

Microsoft C/C++ version 7.0 generates the following warning:

C4001 nonstandard extension used - 'function given file scope

RESOLUTION

Use one of the following methods to avoid the error message:

1. Prototype the function itself and then specify actual parameters

when declaring the pointer, as follows:

type (*ptr)(parameter_list);

ptr = function;

2. Use the warning level flag "/W2" instead of "/W3".

Note: The parameter list must be exactly the same parameter list

with which the function was declared.

More Information:

The following program compiles and links with NO warnings when

compiling with the "/W3"warning level flag set.

Sample Code

-----------

/* Compile options needed: /W3

*/

#include <stdio.h>

void main (void)

{

/* Declare 'fun_ptr' as a pointer to a function */

int (*fun_ptr)(const char *, ... );

int other_args;

/* Assign pointer to the specific function, 'printf' */

fun_ptr = printf;

/* Standard usage in calling environment */

fun_ptr("format string goes here", other_args);

}