8. Pointers: Pointers to functions, Arrays, Strings,
Pointers to Pointers, Array of Pointers
,EX. 8A
Pointer to Function
DATE:
Aim:
To write a C Program declares and accesses pointer to function
Algorithm:
Step 1: Start the Process
Step 2: Define a pointer to a function (*fpointer)
Step 3: Define general functions (add, sub)
Step 4: Put the address of „add‟ in „fpointer‟
Step 5: Execute „add‟ function through fpointer() & pass the two value to add
function and print the result.
Step 6: Put the address of „sub‟ in „fpointer‟
Step 7: Execute „sub‟ function through fpointer() & pass the two value to sub
function and print the result.
Step 8: Stop the process
,#include<stdio.h>
int(*fpointer)(int, int);
int add(int, int);
int sub(int, int);
int main()
{
fpointer = add;
printf(“%d \n”, fpointer(4, 5));
fpointer = sub;
printf(“%d \n”, fpointer(6, 2));
getch();
return 0;
}
int add(int a, int b)
{
return(a + b);
}
int sub(int a, int b)
{
return(a - b);
}
, Input1:
fpointer(4, 5)
Output1:
9
Input2:
fpointer(6, 2)
Output2:
6
Result:
Thus the C Program Pointer to function was executed and the output was
obtained.
Pointers to Pointers, Array of Pointers
,EX. 8A
Pointer to Function
DATE:
Aim:
To write a C Program declares and accesses pointer to function
Algorithm:
Step 1: Start the Process
Step 2: Define a pointer to a function (*fpointer)
Step 3: Define general functions (add, sub)
Step 4: Put the address of „add‟ in „fpointer‟
Step 5: Execute „add‟ function through fpointer() & pass the two value to add
function and print the result.
Step 6: Put the address of „sub‟ in „fpointer‟
Step 7: Execute „sub‟ function through fpointer() & pass the two value to sub
function and print the result.
Step 8: Stop the process
,#include<stdio.h>
int(*fpointer)(int, int);
int add(int, int);
int sub(int, int);
int main()
{
fpointer = add;
printf(“%d \n”, fpointer(4, 5));
fpointer = sub;
printf(“%d \n”, fpointer(6, 2));
getch();
return 0;
}
int add(int a, int b)
{
return(a + b);
}
int sub(int a, int b)
{
return(a - b);
}
, Input1:
fpointer(4, 5)
Output1:
9
Input2:
fpointer(6, 2)
Output2:
6
Result:
Thus the C Program Pointer to function was executed and the output was
obtained.