Thursday, April 14, 2011

Can an array be considered as a pointer ?

        
     Similarities in the way of accessing between arrays and pointers in C/C++ make many programming beginners to conclude - " Arrays are (kind of) Pointers ! ". Possibility of accessing array elements much like pointers is a too easy slip to believe so. But in reality, they are entirely different.  

Mythical reasons

    Reason 1: 
    Operator * is the first culprit. Elements of the array can also be accessed much like dereferencing a pointer. Let's work on with an example -
void foo()
{
    int lVariable = 10 ;
    int *lPtr = &(lVariable);

    int lArray[] = {1,2,3,4,5} ;
    
    std::cout <<*(lPtr) << "\n" ;             // Dereferencing the pointer to get the value at the   
                                              // pointer pointed location.
    for( int i=0; i<5; ++i )
        std::cout <<*(lArray+i) << "\n" ;     // Notice dereferencing an array element much 
                                              // like a pointer.   
}
    Reason 2:
    When an 1D array is passed to (or) returned by a function, it decays to a pointer pointing to the first element of the array. So, " Array is a kind of Pointer ! "
void foo( int *decayPtr ) 
{
    // Access array elements either by operator [] (or) *
}
// Function foo can also be defined differing it's argument semantics as -
//     i) void foo( int decayPtr[5] ) { }
//    ii) void foo( int decayPtr[] ) { }

//    Either way defined will make the compiler convert to pointer type and the size mentioned in the array 
//    index is just ignored by the compiler.
  
int main()
{
    int lArray[] = {1,2,3,4,5} ;
    foo(lArray) ;
}
    The above two situations are compelling enough to conclude either arrays are (a kind of) pointers. No they are not !! Arrays and pointers are no way related.

Reducito ad absurdum
    
    Sometimes it is tough to prove the claim and the process of reverse engineering eases the job. For time being, assume array is (kind of) pointer. If array is a pointer then much like any other raw pointer it can be assigned an address location.
void foo()
{
    int lVariable = 10 ;
    int lArray[] = {1,2,3,4,5} ;
    
    lArray = &(lVariable) ;    // If array is a pointer of any kind, assignment operation
                               // should be valid.    
}
Error: Incompatible types in assignment of 'int*' to 'int[5]'
    The error message is crystal clear enough to say it all and dispel the belief. How ever, when you pass the array to a function it is entirely a different story. The function's argument is pointer itself and so can either point to a different location at the cost loosing the access to array elements if the initial pointer's content is not stored in a temporary variable.
void foo( int *decayPtr )
{
    int lVariable = 10 ;
    decayPtr = &(lVariable) ;     // Perfectly valid because decayPtr is not an array.
                                  // After assignment, it lost the starting address of array element it  
                                  // had initially and noway can be retained back if not stored in a 
                                  // temporary pointer variable.
}
    
Conclusion
    
    Array is just contiguous memory location(s) that can hold similar data types. It's size cannot be modified once allocated. Pointer variable of type "T" can hold the address of variable of type "T". They are just what they are and not even distant cousins.

PS: 
  • The word "decay" is a loosely used term to understand for convenience when array is passed or returned.
  • As always, the disclaimer holds good for this post too :)

No comments:

Post a Comment