
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/