Tuesday, 12 February 2013

C Programming: Pointers

In the simplest term pointer is a nearly integer variable which stores a memory address of a computer which may contain other variable or even another pointer.
If a variable contains address of another variable than it is said that first variable points to second. Pointer can also be represented as a reference to another variable but there is very subtle difference in the two statements which is mostly dependent upon situation and environment.
Pointer is generally of size of integer on the machine but it may be of different type which indicates the type of variable the pointer is pointing to decides pointers properties and behavior. Pointer of one type cannot be implicitly converted from one type to another but can be explicitly converted using type casting. In such a conversion a pointer always assumes that it is point to a object of its type but reality may differ and if used incorrectly may lead to disasters including permanent machine damage.
Also it is important to note that all operation perform on pointers are done through two operators '*' (Star) and '&' (Ampercent). '&' is a unary operator that returns a memory address of a variable. '*' is complement of '&' and return value stored at a memory location stored in a pointer. '*' can interpreted as statement "at address" while '&' can be interpreted as statement "address of".
Pointer Arithmetic
Pointer arithmetic is quite different from normal arithmetic unless and until you are work on char type pointers, reason being they are 1 byte long under all environments.
Not all arithmetic operations are defined in pointers. You can increment them, decrement them, add and subtract integer values from them. You even can subtract two pointers.But you cannot add two pointers, multiply, divide,modulus them. You can not also add or subtract values other than integer.
Now pointer arithmetic may look a little weird but it has a deep sense attach to it.
Now consider a pointer X , its current value that address it is pointing to is 1000 (just assuming).We make another assumption about the size of the data types. Size of data type is machine dependent, for example int can be 16,32, or 64 bit long depending upon your machine.
Now if this X pointer is char type(assumed 1 Byte or 8Bit long) than X++ will have value 1001 and X-- will have value 999. Now if this X pointer is integer type (assumed 2 byte or 32 bit long) than X++ will have value 1002 and X-- will have value 998. Again if this X pointer is float type (assumed 4 Byte or 32Bit long) than X++ will have value 1004 and X-- will have value 9996. Also if this X pointer is double type(assumed 8 Byte or 64 Bit long) than X++ will have value 1008 and X-- will have value 992. Do you see the pattern here.
Reason is when you increment a pointer of certain base type it increase it value in such a way that it points to next element of its base type. If you decrement a pointer its value decrease in such a way that it points to previous value of its base type. So increment as well as decrement in fixed quanta of size of the base type.
You can add or subtract any integer value, in such case value of pointer get increase and decrease by the product of the value to be added or subtract and size of the base type.
Pointer of user defined types such as structures and union also increase by the quanta of their bit values which can be determined using sizeof operator.
Pointer arithmetic in C may look a bit strange but it is extensively used in programming and provides unmatched efficiency in performance of task such as accessing an array.
Declaring Pointer Variables
In C, every variable must be declared for its type. Since pointer variables contain addresses that belongs to a separate data type, they must declared as pointers before we user them. The declaration of pointer variable takes the following form
data_type *pointer_name

This tells the compiler three things about the variable pointer_name.

  1. The asterisk(*) tells that the variable pointer_name is a pointer variable
  2. pointer_name needs a memory address.
  3. pointer_name points to a variable of type data_type.

Initialization of Pointer Variables

Once a pointer variable has been declared we can use the assignment operator to initialize the variable. Example:

  1: int p; 
  2: int *q;  /*declaration */
  3: q = &p;  /*initialization*/

No comments:

Post a Comment