Fun with arrays in C & C++ - Part2
In Part-1 , we saw a couple of examples on array indexing and pointer arithmetic. Lets dive a little deeper in to array types . Arrays & pointers An array is a series of contiguous values in memory and the array name gives us a handle to this list. In C and C++, a pointer refers to (or points to ) another object in memory. Since arrays are guaranteed to be contiguous, if we have a pointer to the first element in the array, we can access the whole array (as long as we know how big it is) Assigning the address of the first element to a pointer: int a[100]; int *p = a; //p now points to a[0] Here p is a pointer to int and we are simply using the array name for assignment. The array name is said to freely decay to a pointer to the object's type . i.e. if a is an array of objects of type T, then it can be seamlessly converted to a pointer to T (i.e. to a T* ). This is what happens when you pass an array's name to a function that takes a poin...