Pointer to a function

December 20th, 2009 by Kevin | 1 Comment | Filed in C Programming

In a C program, we can have a pointer to a function which gives us a pointer to a specific block of executable code. The code to be executed is determined at the run time depending on the address assigned to the function pointer. This helps us create programs which can emulate the polymorphic capabilities of any object oriented programming language.

A function pointer is declared as

return-type (* function-pointer-name)(type arg1, type arg2. ...);

Following code excerpt shows an example how a function pointer can be used to design polymorphism in C.

struct MediaObject_
{
   ...........
   void (*play)(int ); /* declaration of a function pointer */
}MediaObject;

playAudio(int sound)
{
   ............
}

playVideo(int sound)
{
   ............
}

void main()
{
   MediaObject mediaObj;
   MediaType mediaType = getMediaType();

   if(mediaType == AUDIO)
   {
       mediaObj.play = playAudio; /* function pointer assignment */
   }
   else if(mediaType == VIDEO)
   {
      mediaObj.play = playVideo; /* function pointer assignment */
   }

   /* play function pointer can be used for audio or video*/
   mediaObj.play(100);
}

Care should be taken to provide some value to your function pointer before it is actually used. Since a function pointer is a variable, you will not get any compilation errors if you do not initialize it but your program will give an ugly crash at runtime.

 

Related Posts:

Tags: , ,

Call by reference using pointers

December 19th, 2009 by Kevin | No Comments | Filed in C Programming

We have seen that in a C program, the function arguments are call by value by default. Which means that when we pass arguments into a function, a copy of those arguments is created instead of using the actual variables which have been passed.

void foo(int x, int y)
{
   x = 50; /* does not affect the value of a because x is copy of a */
   y = 100; /* does not affect the value of b because y is copy of b */
   return;
}

void main()
{
   int a = 10;
   int b = 20;

   foo(a, b); /* the actual arguments passed to the function are a and b */
   printf("a = %d, b = %d\n"); /* after calling function foo(), value of a is 10 and b is 20 */
   return;
}

So if we need to get the values of a and b modified by the function foo(), we need to use call by reference. Formally, there is no mechanism in C for call by reference. But pointers help us out by providing this functionality.

void foo(int* px, int* py)
{
   *px = 50; /* since px is pointing to a, *px is reference to a */
   *py = 100; /* since py is pointing to b, *py is reference to b */
   return;
}

void main()
{
   int a = 10;
   int b = 20;

   foo(&a, &b); /* the actual arguments passed to the function are a and b */
   printf("a = %d, b = %d\n"); /* after calling function foo(), value of a is 50 and b is 100 */
   return;
}

In the above example, we pass the address of variables a and b to the function foo() which accepts pointers to integer as arguments. So when we reference the variables by using the dereference operator on px and py, we are referencing the actual variables a and b. This causes their value to be modified in the calling function.

This is further simplified in the figure shown below:

Call by reference

An array is handled specially when it is passed as an argument. Instead of copying the entire array, just a pointer to the first element of the array is passed as a parameter (which in fact is passing the array by reference). So any changes made to the array passed as a formal argument will reflect into the actual argument.

void foo(int pArray[]) /* array passed as call by reference. could also be int *pArray */
{
   int i = 0;
   for(i=0; i<10; i++)
   {
      pArray[i] = 100; /* changes the value of array a in the main() */
   }
   return;
}

void main()
{
   int a[10];

   foo(a); /* the actual arguments passed to the function are a and b */
   return;
}
 

Related Posts:

Tags: ,

Pointers and Arrays

December 18th, 2009 by Kevin | No Comments | Filed in C Programming

Array

An array is a collection of items which share the same characteristics. So we can have an array of integers, array of characters, etc. The important point about arrays is that the elements of the array are located into contiguous locations of memory. This can be used to our advantage in programming.

An integer array is defined as int a[5];

Array

As the diagram above shows, we have defined an array of type integer with six elements in it. The elements of the array are stored sequentially in the memory as shown. Hence they can be accessed by their index as a[0](first element), a[1](second element), a[2], …

Since this array is of type integer, every element is an integer and so each occupies four bytes (depends on the machine) of memory.

A character array is a special kind of array which is also known as a string. What makes the character array special is that it can be used by several functions in the string manipulation library (and also by the user defined functions) because of it’s characteristic of placing a ‘\0′ escape sequence at the end of the array to indicate the string termination.

Pointers

A pointer is a variable which holds the memory address of another variable. We can have a pointer to any data type we want such as integers, floats, characters, etc. We can also have pointers to other abstract data types that we create (which we will see in later posts).

Pointer Variables

As shown in the figure, p is a pointer variable which points to the address of an integer variable a. The following example will make it more clear.

int *p; /* this is the declaration of the integer pointer. */
int a = 10;

p = &a; /* Here the address of variable a is assigned to pointer p by using the reference operator */
printf("a = %d\n", *p); /* Here a is shown to be same as *p where * is the dereference operator */

And then the pointer can be assigned the address of any integer variable as

p = &a;

& is the referencing symbol and * is the de-referencing symbol for the pointer.

Pointers and Arrays

An array is a sequence of elements stored in a contiguous block of memory. So we can consider an array a of 10 elements by representing it with a pointer as shown

int a[10];

int *p = &a[10]; /* pointer to an array */

a[5] = 10;

*(p + 5) = 10; /* same as above statement */

p[5] = 10; /* same as above two statements */

When an array is passed into a function, instead of a copy of the array just a unmodifiable pointer to the first element of the array is passed.

In the next post, I will write about such uses of pointers including others such as call by reference, function pointers.

 

Related Posts:

Tags: , ,