Skip to main content
Engineering LibreTexts

2.4: Array Name as Pointers

  • Page ID
    34649
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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


    This page titled 2.4: Array Name as Pointers is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.

    • Was this article helpful?