Home C++ Introduction Decisions Loops Input/Output Functions Stack and Heap References Arrays Searching and Sorting Recursion Pointers Character and Strings Structures Classes Inheritance Exceptions Templatess STL Modern C++ Misc Books ----

Arrays and Pointers



As we saw arrays and pointers are very similar. Just as a block of memory can be allocated for a pointer and the starting address assigned to a pointer, a block of memory is allocated for an array in the same fashion.
If we say
int array1[10] ;
Then a block of 10 integers is allocated in RAM and the starting address is assigned to the variable "array1" . The key difference between array variables and pointer variables is that a pointer variable's value can be changed but an array variable's value can never be changed. We can access elements in an array using pointer notation and vice versa.
We can obtain the address of the array variable in many ways.

File: arrays1.cpp
/* Array address*/
#include<stdio.h>
#include <stdlib.h>


int main()
{
    int array1[] = {100,200,300} ;
    int* ptr1 ;

    printf( "%p\n" , array1)  ;
    printf( "%p\n" , &array1)  ;
    printf( "%p\n" , &(array1[0]) )  ;

    return 0 ;
}
The above program has an array declared with the name "array1" . A block of 3 elements is allocated and the address assigned to "array1" . There are many different ways of getting the address. We can just use the array name "array1" and that is probably the easiest way to obtain the address. We can use "&array1" that does the same thing. Since the first element is at the starting address and if we take the address of that using the notation "&(array1[0])" then we have the same address also.
Next example will show how an array can be treated as a pointer and vice-versa.

File: arrays2.cpp
/* Array and pointers */

#include<stdio.h>
#include <stdlib.h>


int main()
{
  int array1[] = {100,200,300} ;
  int* ptr1 ;

  //An array can be treated as a pointer
  //array1 = ptr1 ;

  ptr1 = array1  ;
  printf( "%d\n" , *ptr1 ) ;
  printf( "%d\n" , *(ptr1+1) ) ;
  printf( "%d\n" , *array1 ) ;
  printf( "%d\n" , *(array1+1) ) ;

  // A pointer can be treated as an array.
  ptr1 = (int*)malloc( sizeof(int) * 10  ) ;
  *ptr1 = 10 ;     *(ptr1+1) = 20 ;
  printf( "%d\n" , ptr1[0] ) ;
  printf( "%d\n" , ptr1[1] ) ;
  free ( ptr1) ;

 }
As the above example shows we can have an array variable "array1" . We can access elements using "*array1" and "*(array1+1)" .
As we stated earlier a pointer can hold the address of a single element or a block. When it holds the address of the block then it acts like an array. We can also think of a pointer as an array. What about an array of pointers ? Each element is a pointer and if we think of that as an array , then we have a array of arrays or in other words a 2 dimensional array.
Cout and Addresses

File: c1.cpp
#include <iostream>
using namespace std ;

int main()
{
    char* str1 = "string1" ;
    char  str2[] = "string2" ;

    char ch = 'A' ;
    int x1 = 100 ;

    cout  << str1 << endl ;
    cout  << str2 << endl ;
    cout  << ch << endl ;
    cout  << &ch << endl ;
    cout << "Address of str1:" << (void*)str1  << endl ;
    cout << "Address of str2:" << (void*)str2  << endl ;
    cout << "Address of ch:" << (void*)&ch  << endl ;
    cout << "Address of x1:" << &x1  << endl ;

}
Output:
string1
string2
A
Astring2
Address of str1:0x400ac5
Address of str2:0x7ffe61baa0d0
Address of ch:0x7ffe61baa0cf
Address of ch:0x7ffe61baa0c8