Created
February 26, 2012 16:47
-
-
Save sinannar/1917621 to your computer and use it in GitHub Desktop.
Function Pointer Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* FUNCTION POINTERS | |
* Created By :Sinan NAR | |
* Creation Date :26/02/2012 | |
* | |
* you can ask me anything about these codes if you do not understand | |
* mail me : [email protected] | |
*/ | |
#include <stdio.h> | |
/* | |
* Using function pointer in struct | |
* We need to forwarddeclaration of this struct cause of fnction | |
* prototype of func7 | |
*/ | |
typedef struct structExmp; //forward decleration | |
void func7(structExmp); //function prototype | |
typedef struct{ //struct decleration | |
int num; | |
void (*printfNum1)(structExmp) ; | |
}structExmp; | |
/* FUNCTION PROTOTYPES */ | |
void func1(int); | |
void func2(int,int); | |
void func3(int); | |
void func4(char); | |
void func5(char,char); | |
char* func6(int,char*); | |
/* MAIN */ | |
int main(void) | |
{ | |
/* FUNCTION POINTERS DECLERATIONS */ | |
void (*funcPtr1)(int); | |
void (*funcPtr2)(int,int); | |
void (*funcPtr3)(char); | |
void (*funcPtr4)(char,char); | |
char* (*funcPtr5)(int,char*); | |
/* STRUCT DECLERATION*/ | |
structExmp x; | |
/* INITIALIZATION */ | |
x.printfNum1=func7; | |
x.num=2; | |
funcPtr1=func1; | |
funcPtr2=func2; | |
funcPtr3=func4; | |
funcPtr4=func5; | |
funcPtr5=func6; | |
/* Function calls and tests */ | |
funcPtr1(1); | |
funcPtr1=func3; | |
funcPtr1(2); | |
funcPtr2(1,2); | |
funcPtr3('a'); | |
funcPtr4('a','b'); | |
funcPtr5(5,"sinan"); | |
/* Struct test */ | |
x.printfNum1(); | |
return 0; | |
} | |
/* FUNCTION DECLERATIONS */ | |
void func1(int num1) | |
{ | |
printf("func1 called and print %d on screen\n",num1); | |
} | |
void func2(int num1,int num2) | |
{ | |
printf("func2 called and print %d and %d on screen\n",num1,num2); | |
} | |
void func3(int num1) | |
{ | |
printf("func3 called and print %d on screen\n",num1); | |
} | |
void func4(char var1) | |
{ | |
printf("func4 called and print %c on screen\n",var1); | |
} | |
void func5(char var1,char var2) | |
{ | |
printf("func5 called and print %c and %c on screen\n",var1,var2); | |
} | |
char* func6(int num1,char* word1) | |
{ | |
printf("func6 called and print %d and %s on screen\n",num1,word1); | |
return word1; | |
} | |
void func7(structExmp var) | |
{ | |
printf("func7 called and print %d on screen\n",var.num); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment