Microsoft KB Archive/70142

= INFO: Function Pointers to Functions with Different Parameters =

Article ID: 70142

Article Last Modified on 12/12/2003

-

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++ 5.0 Professional Edition

-



This article was previously published under Q70142





SUMMARY
The sample code below illustrates how to use function pointers to call functions that take different parameters than those with which the pointer was declared. The function is cast to the type of the function pointer during the first assignment, then the function pointer is cast to the type of the function to be called. Using typedefs generally makes it easier than casting the pointer directly.

Sample Code
/* Compile options needed: none


 * 1) include 

typedef int (*myintchartype) (char *, char *); typedef void (*myvoidtype) (void); typedef int (*myintvoidtype) (void);

int func1(char *, char *); void func2(void);

void main(void) {  myintvoidtype ptr;

ptr = (myintvoidtype) func1; ((myintchartype) ptr)(&quot;one&quot;, &quot;two&quot;);

ptr = (myintvoidtype) func2; ((myvoidtype) ptr); }

int func1(char *a, char *b) {  return printf(&quot;func1 took two parameters: %s and %s\n&quot;, a, b); }

void func2(void) {  printf(&quot;func2 did not take any parameters\n&quot;); }

Additional query words: argument list

Keywords: kbinfo kblangc KB70142

-

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

© Microsoft Corporation. All rights reserved.