• Now Online : 68
  • admin@codemyne.net

Introduction

Very often in programming it is required to declare a pointer to an array or to a function or to any combination of pointers, arrays and functions. This chapter deals with such usage of pointers. A C-declaration may include various combinations of pointers, arrays and functions. To parse a complex declaration, the compiler has to sort out the attributes listed below:

Attribute Meaning
() function
[] array
* Pointer

Right-Left Rule:

  • Start with the identifier
  • Look to the right, for an attribute
  • If none is found, look to the left
  • If found, substitute English Keyword
  • Continue right-left substitutions
  • Stop when you reach the data type

Example: int(*pf[5]) ()

  • pf is the identifier
  • Right: pf is an array
  • Left: pf is an array of pointers
  • Right: pf is an array of pointers to functions
  • Left: pf is an array of pointers
  • Right: each array element is the address of a function that returns an integer.

int*(*pf[5]) ()

struct block* (*table()) [10];

  • Table is the identifier.
  • Looking right, table is a function.
  • Looking left, the function returns a pointer.
  • Looking right, the function returns a pointer to an array.
  • Looking left again, each array element is a pointer to a structure.

To make a declaration for an array of 10 pointers to functions that return pointers to integers.

buf[10] --> array of 10
*buf[10] --> array of 10 pointers
(*buf[10])() --> array of 10 pointers to functions
int *(*buf[10])() --> array of 10 pointers to a functions returning integer pointers.

Pointers to Functions

C language has a data type that corresponds to a function pointer. To declare a variable called fun_pointer, which points to a function that returns an integer:

int (*fun_pointer) ();

Notice the first pair of parentheses around *fun_pointer. If these were not present, the declaration would be

int *fun_pointer();

Which would mean, fun_pointer is the name of a function that returns a pointer to an integer. To declare a variable point_point, which points to a function that returns a pointer to an int:

int *(*point_point) ();

If we want to assign address of a function, say strlen(), to the pointer variable fun_pointer, we must explicitly declare the function strlen() as ( int strlen(); ) even if it returns an int. Now the address of strlen() can be assigned to fun_pointer:

fun_pointer=strlen;

To call strlen() indirectly through fun_pointer, one would write:

(*fun_pointer) (a);       // where 'a' is a string

To pass a function address to another function.

  • either pass the value of a function pointer variable, Ex.. multi(*fun_pointer);
  • or the function name (without parentheses) Ex..multi(strlen);

Example: The following program prints an integer in decimal and hexadecimal by calling a function (convrt_prt()) which takes a pointer to the appropriate function (print_int(), or print_hex()) as an argument.

main()
{
    int print_int();
    int print_hex();
    convrt_prt(‘5’, print_int);
    convrt_prt(‘5’, print_hex);
}
convrt_prt(value, print_func)
int value;
int (*print_func)();
/*print_func is a variable that points to a function that returns an integer*/
{
    value = value-5;
    (*print_func)(value);
}
print_int(value)
    int value;
{
    printf(“The value received was %d in decimal \n ”, value);
} 
printf_hex(value)
int value;
{
    printf(“The value received was %0x In hex \n”, value);
}

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment