Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

12.4: Arrays, Pointers and Such

( \newcommand{\kernel}{\mathrm{null}\,}\)

  Difference between pointer and array in C++?

Pointers are used for storing address of dynamically allocated arrays and for arrays which are passed as arguments to functions. In other contexts, arrays and pointer are two different things, see the following programs to justify this statement.

The behavior of the sizeof operator is shown below

/ 1st program to show that array and pointers are different 
#include  
using namespace std; 

int main() { 
   int arr[] = {10, 20, 30, 40, 50, 60}; 
   int *ptr = arr; 

   // sizof(int) * (number of element in arr[]) is printed 
   cout << "Size of arr[] "<< sizeof(arr)<<"\n"; 

   // sizeof a pointer is printed which is same for all type  
   // of pointers (char *, void *, etc) 
   cout << "Size of ptr "<< sizeof(ptr); 

   return 0; 

} 

The array is 6 members * 4 bytes each, so we get 24 bytes for the variable arr.  The pointer ptr, and all pointers, are 8 bytes, because they hold addresses, which are 8 bytes, or 64 bits.

Output:

Size of arr[] 24
Size of ptr 8

Assigning any address to an array variable is not allowed.

// Second program to show that array and pointers are different 
#include <iostream>
using namespace std; 

int main() 
{ 
    int arr[] = {10, 20}, x = 10; 
    int *ptr = &x; // This is fine because ptr is to hold addresses
    arr = &x; // Compiler Error this is an integer array, not meant to hold addresses
    return 0; 
}

Output:

 Compiler Error: incompatible types when assigning to 
              type 'int[2]' from type 'int *' 

Although array and pointer are different things, following properties of array make them look similar.

  • Array name gives address of first element of array.
    Consider the following program for example.
// 1st program to show that array and pointers are different 
#include <iostream> 
using namespace std; 

int main() { 
   int arr[] = {10, 100, 200, 40, 50, 60}; 
    
   // Assigns address of array to ptr 
   int *ptr = arr; 
   cout << "Value of first element is " << *ptr; 
   
   return 0; 
} 

In the above code the value of arr is actually an address. From that address all of the members of the array are calculated - an int is 4 bytes, so the value of 10, as shown in the array showing address arithmaticimage (the image uses an array named v, instead of arr), is at specific address. The next value 100 is at that same address PLUS 4 - because integers are 4 bytes long. So, by saying arr[1] we are saying - the value we want, 100, is at the address of arr + 4 more bytes - because the value of 10 takes up the 4 bytes from the address 0x7fff9a9e7920 to 0x7fff9a9e7923

So really we are simply performing address arithmatic - it all depends on the type of the array, which determines how much memory each value takes up.

Output:

Value of first element is 10 
  • Array members are accessed using pointer arithmetic.
    Compiler uses pointer arithmetic to access array element. For example, an expression like “arr[i]” is treated as *(arr + i) by the compiler. That is why the expressions like *(arr + i) work for array arr, and expressions like ptr[i] also work for pointer ptr.
    #include <iostream> 
    using namespace std; 
    
    int main() { 
       int arr[] = {10, 20, 30, 40, 50, 60}; 
       int *ptr = arr; 
       cout << "arr[2] = "<< arr[2] <<"\n"; 
       cout << "*(arr + 2) = "<< *(arr + 2)<<"\n"; 
       cout << "ptr[2] = "<< ptr[2]<< "\n"; 
       cout << "*(ptr + 2) = "<< *(ptr + 2)<< "\n"; 
       return 0; 
    } 
    
  • Output:

    arr[2] = 30
    *(arr + 2) = 30
    ptr[2] = 30
    *(ptr + 2) = 30 
  • Array parameters are always passed as pointers, even when we use square brackets.filter_none
    #include <iostream>
    using namespace std; 
    
    int fun(int ptr[]) 
    { 
       int x = 10; 
    
       // size of a pointer is printed 
       cout << "sizeof(ptr) = " << sizeof(ptr) << endl; 
    
       // This allowed because ptr is a pointer, not array 
       ptr = &x; 
    
       cout << "*ptr = " << *ptr << endl; 
    
       return 0; 
    } 
    
    // Driver code 
    int main() 
    { 
       int arr[] = {10, 20, 30, 40, 50, 60}; 
       fun(arr); 
       return 0; 
    } 
    

     

    Output:

    sizeof(ptr) = 8
    *ptr = 10

 

Array Name as Pointers

An array name contains the address of first element of the array which acts like constant pointer. It means, the address stored in array name can’t be changed.
For example, if we have an array named val then val and &val[0] can be used interchangeably.

// C++ program to illustrate Array Name as Pointers in C++ 
#include <iostream> 
using namespace std; 
int main() 
{ 
    //Declare an array 
    int newArr[3] = { 5, 10, 20 }; 
    
    //declare pointer variable 
    int *arrPtr; 
    
    //Assign the address of newArr[0] to ptr 
    // We could use arrPtr = &newArr[0];(both are same) 
    arrPtr = newArr ; 
    cout << "Elements of the array are: "; 
    cout << arrPtr[0] << " " << arrPtr[1] << " " << arrPtr[2]; 

    return 0;
}

In looking at this code you may notice that when when we use the square brackets - [ ] - it is actually using the address. This is because the value stored in the newArr variable IS an address in memory. 

Output:
Elements of the array are: 5 10 20

This page titled 12.4: Arrays, Pointers and Such is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.

Support Center

How can we help?