Pointers:
A pointer is basically an address of any memory location. Precisely it is a variable which contains address of another variable.
int var = 10; // Creates a variable by reserving memory. int * p; // Creates a pointer whose type is integer. It can only hold address // of another variable - It does not contain any address yet. p = &var; // Now p is assigned address of var.
Now we can reference the value 10 in two ways - Using var directly or through p indirectly:
printf("Through Variable var: %d Through Pointer p %d", var, *p);
The * operator tells the compiler to "go to that address in the pointer variable and read value". & operator means "pick address of this variable" (which follows the operator).
We can also indirectly modify the value i.e.
*p = 5;
printf("%d %d", var, *p); // both would print 5
printf("Address contained in pointer is %p\n", p); // Note %p used for pointer and p
without *. This prints a hexadecimal address of memory something like 0xabbde21f
The reason is, there only exist a single memory which is pointed to be var as well as p the pointer. So that memory can either contain 5 or 10. If we change it through pointer, the variable would also read the new value.
We can use p++ or p-- to move around in memory. But to grab that let's begin with arrays:
Arrays
Arrays are a way of group many variables of a same type.
#define SIZE 10 int array[SIZE]; // SIZE is replaced with 10 at compile time.
It says create 10 variables of integer type which are placed consecutively in memory and can be addressed using array[0], array[1] and so on.
So we can write a loop to go from array[0] to array[9] (First element's index is 0 and last is SIZE-1).
Now print 10 elements of array:
for(int cnt=0; cnt < SIZE; ++cnt)
printf("Element Num %d is %d \n", cnt, array[cnt]);
Instead of this we could use pointers too:
int * p = array; // Name of array is already an address, so it does not need & operator
for(int cnt=0; cnt <SIZE; ++cnt) {
printf("Elmt Num %d is %d \n",cnt, *p);
p++;
}
The statement p++ is moving around the pointer. Increasing the address one integer element at a time. So one increment takes it to second array element and so on.
Pointer's size is the size of int on that machine i.e. 4 byte on 32 bit machine and 8 byte on 64 bit machine. If all pointers are addresses, why does it need to have a type i.e. char * p, int * p, float * p are all same sized variables containing addresses then what is the significance of type?
It is the type which tells when we say p++, then how many bytes it actually skips. The above is an integer array so we declare an int pointer. Therefore when we say p++ it skips 4 bytes (assuming int is 4 bytes here). Had it been a char array and the pointer is char * p, p++ would only skip one byte.
Pointer notation of arrays
When we write array[0] or array[1] Compiler is doing a translation into addresses or pointers. We can do that translation too thereby reducing work done by compiler.
Above print statement could also be written as:
printf("Elmt Num %d is %d \n",cnt, *(p + cnt));
So we no longer need to increment p each time. This is actually translating the earlier statement. p is pointer to array (which is pointing to arrays first element). Now each time we add cnt's current value p skips 4 bytes for each single cnt i.e. 4 bytes for cnt 1, 8 for cnt 2, 12 for cnt 3, 16 for cnt 4 and so on.
The above code is faster than the earlier though here it is a slight advantage. But we are preventing compiler from doing one translation by doing it ourselves.
This relation is extended into double or even triple pointers. 2D or 3D arrays are conceptually translated into those but should be addressed separately.
Edited by fayyazlodhi, 16 May 2011 - 01:37 PM.
replacing constant with Macro


Sign In
Create Account


Back to top









