Pointer Functions and Pointers to Pointer Functions 
Author Message
 Pointer Functions and Pointers to Pointer Functions

/ /***   VERSION 2 ***/
/
/ funptr.c :
/
/ #include <stdio.h>
/ #include "funptr2.h"
/
/ void name(char *nom){
/     printf("NAME:: %s\n",nom);
/ }

type is sort of (void ()(char*))

/ void age(int years){
/    printf("AGE :: %d\n",years);
/ }

type is sort of (void ()(int))

/
/ void gender(char mf, char *sex){
/    if (mf == 'm')
/       printf("Male :: %s\n",sex);
/    else
/       printf("Female :: %s\n",sex);
/ }

type is sort of (void ()(char,char*))

/ int main(void){
/    void *((*p)());                        /* pointer to a function pointer
/ */
/    void *((*p2)()) = NULL;
/    void *((*p3)());

No, these are pointers to a function (with unknown arguments) which
returns a void*.

/
/    p = (void *) name;
/    p ("Hello there");

Casting name to (void*) turns off pointer type checking, presumably
necessary because you're using the wrong type. To make the assignment
without a cast,

    void (*p)(char*);

/    p = (void *) age;
/    p (25);
/
/    p = (void *) gender;
/    p('M',"Male");
/
/    p2 = p('F',"Hello");
/
/    p2;

Loads and then discards the value of p2. To force a call you must include
an argument list even if it's empty.

/    p3 = p2;
/
/    name2();    
/ }
/
/ funptr2.c:
/ #include <stdio.h>
/
/    extern void *((*p3)());
/
/ void name2(void){
/
/    printf("Before Function\n");
/    p3;                          /* This does nothing */

Loads and then discards the value of p3. To force a call you must include
an argument list even if it's empty.

/    printf("After Function\n");
/
/ }
/
/ funptr2.h:
/ void name2(void);

--
Collective Against Consensual Sanity
v0.1 is sort of up.   http://www.*-*-*.com/
For a free CACS T-shirt send $42 shipping and handling.
pretty?     http://www.*-*-*.com/



Sun, 10 Mar 2002 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. How to use (pointer to function), and function and pointer to (pointer to function)

2. Question about signal()/pointers to functions that return pointers to functions

3. function pointers and function pointers array

4. C++ function pointers versus C function pointer problem

5. Pointer to function returning pointer to function returning...

6. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

7. Pointers to Structure that contains pointer to Function

8. Casting function pointer to void pointer

9. Pointer to an array of pointers to functions

10. Assigning structure pointer to function pointer

11. pointers to functions and pointers to void.

12. pointers to functions in structs which take a pointer to that struct as an arg

 

 
Powered by phpBB® Forum Software