Posts

Showing posts from May, 2014

Difference between Array and Pointers

One and only difference between an array and a pointer pointing to the beginning of the array is that the compiler keeps some extra information for the arrays, to keep track of their storage information. Lets understand it more with the example by using the sizeof operator :  If we get the size of both an array and a pointer using the sizeof  operator, sizeof(ptr) will give us how much space does the pointer itself occupies (4 in case of int) while sizeof array will give us, the amount of space occupied by the whole array (if array contains 10 elements of int data-type (2 bytes) , then this operator will give 20). In general, *(array+n) is equivalent to array[n].

Void Pointers

Limitation of Void Pointers: 1. They can't be dereferenced.     Reason:  Each variable type takes different amount of memory. So, in order to read the actual value stored there, the compiler has to know how many consecutive memory locations to read in order to get the full value. Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator. 2. We can't perform arithmetic operations on them.   Reason:  The compiler cannot know how many bytes ahead the next variable is located. Void pointers are mainly used to keep addresses that we have to convert later on to a specific pointer type, before using them.