Skip to main content
Engineering LibreTexts

2.4: Array Name as Pointers

  • Page ID
    34649
  • 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 call-by-methods 
    #include <iostream> 
    using namespece std;
      
    int main()  
    {  
        //Declare an array  
        int val[3] = { 5, 10, 20 };  
          
        //declare pointer variable  
        int *ptr;  
          
        //Assign the address of val[0] to ptr  
        // We can use ptr=&val[0];(both are same)  
        ptr = val ;  
        cout << "Elements of the array are: ";  
        cout << ptr[0] << "  " << ptr[1] << "  " << ptr[2] << endl;  
    
         return 0;
    } 

    Output:

    Elements of the array are: 5   10   20

    How are pointers and arrays similar

    If pointer ptr is sent to a function as an argument, the array val can be accessed in a similar fashion.

    Adapted from:

    "Pointers in C/C++ with Examples" by Abhirav Kariya, Geeks for Geeks is licensed under CC BY 4.0

    • Was this article helpful?