xxxxxxxxxx
#include <stdio.h>
#include <string.h>
void (*StartSd)(); // function pointer
void (*StopSd)(); // function pointer
void space()
{
printf("\n");
}
void StopSound() // funtion
{
printf("\nSound has Stopped");
}
void StartSound() // function
{
printf("\nSound has Started");
}
void main()
{
StartSd = StartSound; // Assign pointer to function
StopSd = StopSound; // Assign pointer to function
(*StartSd)(); // Call the function with the pointer
(*StopSd)(); // Call the Function with the pointer
space();
StartSd(); // Call the function with the pointer
StopSd(); // Call the function with the pointer
space();
StartSound(); // Calling the function by name.
StopSound(); // Calling the function by name.
}
xxxxxxxxxx
datatype *var;
variable var actually holds the address of the data(memory where it is stored)
*var lets you access the data stored at that address
xxxxxxxxxx
#include <stdio.h>
int main () {
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var );
/* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip );
/* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
function pointer in c
xxxxxxxxxx
general syntax: rtype (*pname)(atype);
with arguments: void (*my_ptr)(int)
without arguments: void (*my_ptr)()
xxxxxxxxxx
// Basic syntax
ret_type (*fun_ptr)(arg_type1, arg_type2, , arg_typen);
// Example
void fun(int a)
{
printf("Value of a is %d\n", a);
}
// fun_ptr is a pointer to function fun()
void (*fun_ptr)(int) = &fun;