Pointers

Pointers are special objects for holding (or “pointing to”) memory addresses. In the mikroC PRO for PIC, address of an object in memory can be obtained by means of an unary operator &. To reach the pointed object, we use an indirection operator (*) on a pointer.

A pointer of type “pointer to object of type” holds the address of (that is, points to) an object of type. Since pointers are objects, you can have a pointer pointing to a pointer (and so on). Other objects commonly pointed to include arrays, structures, and unions.

A pointer to a function is best thought of as an address, usually in a code segment, where that function’s executable code is stored; that is, the address to which control is transferred when that function is called.

Although pointers contain numbers with most of the characteristics of unsigned integers, they have their own rules and restrictions for declarations, assignments, conversions, and arithmetic. The examples in the next few sections illustrate these rules and restrictions.

Pointer Declarations

Pointers are declared the same as any other variable, but with * ahead of identifier. A type at the beginning of declaration specifies the type of a pointed object. A pointer must be declared as pointing to some particular type, even if that type is void, which really means a pointer to anything. Pointers to void are often called generic pointers, and are treated as pointers to char in the mikroC PRO for PIC.

If type is any predefined or user-defined type, including void, the declaration

type *p;   /* Uninitialized pointer */

declares p to be of type “pointer to type”. All scoping, duration, and visibility rules are applied to the p object just declared. You can view the declaration in this way: if *p is an object of type, then p has to be a pointer to such object (object of type).

  Note : You must initialize pointers before using them! Our previously declared pointer *p is not initialized (i.e. assigned a value), so it cannot be used yet.
        In case of multiple pointer declarations, each identifier requires an indirect operator. For example:

Once declared, though, a pointer can usually be reassigned so that it points to an object of another type. The mikroC PRO for PIC lets you reassign pointers without typecasting, but the compiler will warn you unless the pointer was originally declared to be pointing to void. You can assign the void* pointer to the non-void* pointer – refer to void for details.

Null Pointers

A null pointer value is an address that is guaranteed to be different from any valid pointer in use in a program. Assigning the integer constant 0 to a pointer assigns a null pointer value to it.

For example:

int *pn = 0;      /* Here's one null pointer */

/* We can test the pointer like this: */
if ( pn == 0 ) { ... }

The pointer type “pointer to void” must not be confused with the null pointer. The declaration

void *vp;

declares that vp is a generic pointer capable of being assigned to by any “pointer to type” value, including null, without complaint.

Assignments without proper casting between a “pointer to type1” and a “pointer to type2”, where type1 and type2 are different types, can invoke a compiler warning or error. If type1 is a function and type2 isn’t (or vice versa), pointer assignments are illegal. If type1 is a pointer to void, no cast is needed. If type2 is a pointer to void, no cast is needed.

Function Pointers

Function Pointers are pointers, i.e. variables, which point to the address of a function.

// Define a function pointer
   int (*pt2Function) (float, char, char); 
  Note : Thus functions and function pointers with different calling convention (argument order, arguments type or return type is different) are incompatible with each other.

Assign an address to a Function Pointer


It's quite easy to assign the address of a function to a function pointer. Simply take the name of a suitable and known function. Using the address operator & infront of the function's name is optional.
   //Assign an address to the function pointer

  int DoIt  (float a, char b, char c){ return a+b+c; }
  pt2Function = &DoIt;  // assignment              
  

Example:


int addC(char x,char y){

  return x+y;
}

int subC(char x,char y){

  return x-y;
}

int mulC(char x,char y){

  return x*y;
}


int divC(char x,char y){

  return x/y;
}


int modC(char x,char y){

  return x%y;
}


//array of pointer to functions that receive two chars and returns int
int  (*arrpf[])(char,char) = { addC ,subC,mulC,divC,modC};


int res;
char i;
void main() {

   for (i=0;i<5;i++){
     res = arrpf[i](10,20);
   }

}	
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code