-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionpointer_1.c
More file actions
31 lines (16 loc) · 909 Bytes
/
functionpointer_1.c
File metadata and controls
31 lines (16 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
//We are going to use a function pointer to call a function.
int n(int a, int b){ //This is a function that takes two integers as arguments and returns an integer.
return a + b; //This function returns the sum of the two integers.
}
int main(){
int (*p)(int a, int b); //declaring a function pointer. Spot the *. The * tells us that its a pointer
int (*say) (const char *); //declaring a function pointer. Spot the *. The * tells us that its a pointer
say = puts; //assigning the address of the function puts to the function pointer say
say("Hello World"); //calling the function puts using the function pointer say
p = n; //assigning the address of the function n to the function pointer p
printf("%d", p(2, 3)); //calling the function n using the function pointer p
return 0; //returning 0
}