USING *POINTER TO A FUNCTION IN C PROGRAMMING WITH TYPEDEF

USING POINTER TO A FUNCTION IN C PROGRAMMING WITH TYPEDEF

First of all you have to know about how to declare pointer to a function.

/*Declaring a Pointer to a Function Rules*/

Prototype: type (*ptr_to_func)(parameter_list);

Example:

void (*func1)();
char (*func2)(char *p[]);
void (*func3)(double y, double z);
int (*func4)(int x);

Okay that is how you declare pointer to a function. A data type followed by function name in parenthesis() followed by parameter list?

Why do you need parentheses around the pointer name?

Why can’t you write, for the fourth example:
int *func4(int x); The reason has to do with the precedence of the indirection operator, *. It has a relatively low precedence, lower than the parentheses surrounding the parameter list. The declaration WITHOUT PARENTHESIS, you declares func1 as a function that returns a pointer to type int.

HERE’S THE FOLLOWING EXAMPLES OF USING POINTER TO A FUNCTION CALL THEM AND THE USE OF TYPEDEF IN C PROGRAMMING.

/* Using a pointer to a function to call the function */

#include <stdio.h>

int call_a_func(int(*p)(int)){
   int output = p(3);
   return output;
}

int square(int x) /* The function definition. */
{
   return x * x;
}

main(){

  int answer = call_a_func(&square); //call the function square above.
  printf("%d", answer);

  return 0;
}

OUTPUT: 9 // the square root of 3

/*
NOW COMPARE THE ABOVE PROGRAM HERE WHEN I USE the typedef.

Using a pointer to a function to call the function using [typedef]

What is typedef in C?

typedef = used to CREATE NEW NAME OR ALIAS for existing variable
and function types and structure (struct).
*/

#include <stdio.h>

typedef int (*funcX)(int); //see the use of typedef before int.

int call_a_func(funcX anyName){  //funxX point to anyName() function
   int output = anyName(3);
   return output;
}

int square(int x) /* The function definition. */
{
   return x * x;
}

main(){

  int answer = call_a_func(&square); //see the name of a function called (&square)
  printf("%d", answer);

  return 0;
}

OUTPUT: 9 // the square root of 3

/* Simple Pointer to a function without using a caller call_a_func()*/

#include <stdio.h>

int add(int x, int y, int z) /* The function definition.*/
{
  return x + y + z;
}

int (*p)(int, int, int); /* The pointer declaration. */

main(){

int x, y, z, answer; //let us declare 4 int variables;

x = 10;
y = 20;
z = 30;

p = add; /* Initialize pointer p to point to add() function. */

/* Then you can call the function using the pointer as follows: */

answer = p(x, y, z); /* see this add() function become p*/

printf("%d", answer);

return 0;
}

OUTPUT: 60

/* note the above example I did not include function prototype because the
function is writtent above main() function
here’s the code with prototype.
*/

#include <stdio.h>

int add(int x, int y, int z); /* function prototype do not forget ;. */
int (*p)(int, int, int);      /* The pointer declaration. */

main(){

int x, y, z, answer; //let us declare 4 int variables;

x = 10;
y = 20;
z = 30;

p = add; /* Initialize pointer p to point to add() function. */

/* Then you can call the function using the pointer as follows: */

answer = p(x, y, z); /* see this add() function become p*/

printf("%d", answer);

return 0;
}

/* this function definition the same format in prototype above 
  just replace ; by {} and put your block of statements inside 
*/

int add(int x, int y, int z) /* The function definition.*/
{
  return x + y + z;
}

OUTPUT: 60

/* LET US USE typedef TO CHANGE THE WORD “int” TO THE WORD “integer”; */

#include <stdio.h>

typedef int integer;

integer add(integer x, integer y, integer z); /* function prototype do not forget ;. */
integer (*p)(integer, integer, integer);      /* The pointegerer declaration. */

main(){

/* let us declare 4 integer variables; */

integer x;
integer y;
integer z;
integer answer; 

x = 10;
y = 20;
z = 30;

p = add; /* Initialize pointegerer p to pointeger to add() function. */

/* Then you can call the function using the pointegerer as follows: */

answer = p(x, y, z); /* see this add() function become p*/

printf("%d", answer);

return 0;
}

/* this function definition the same format in prototype above 
  just replace ; by {} and put your block of statements inside 
*/

integer add(integer x, integer y, integer z) /* The function definition.*/
{
  return x + y + z;
}

OUTPUT: 60

Advertisements
This entry was posted in C. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s