function ptr in function parameter code example

Example 1: how to use a pointer as a parameter in c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void add(int* a, int* b, int* c)
{
    *c = *a + *b;
}
int main()
{
	int a, b, c;
	a = 3;
	b = 5;
	add(&a, &b, &c);
	printf("%d", c);
}

Example 2: Function pointer C++

void one() { cout << "One\n"; }
void two() { cout << "Two\n"; }


int main()
{
	void (*fptr)(); //Declare a function pointer to voids with no params

	fptr = &one; //fptr -> one
	*fptr(); //=> one()

	fptr = &two; //fptr -> two
	*fptr(); //=> two()

	return 0;
}

Tags:

Misc Example