Home     Blog

Pointers and Arrays

Arrays and pointers are very closely linked together. In most contexts, C++ treats the name of an array as if it were a pointer i.e. memory address of some element. C++ interprets an array name as the address of its first element. That is, if age is an int array to hold 10 integers then age stores the address of age[0], the first element of the array. Consider the following code snippet:

In the above code, a is a pointer to integer and age is an array holding 10 integers. The pointer a is made to point to where age is pointing to. When the data values of these pointers are printed, both values come out to be the same. Thus from the above given code, it is proved that the name of an array is actually a pointer that points to the first element of the array.

Since the name of the array is a pointer to its first element, the arrayname + 1 gives the address of the second element of the array, arrayname + 2 gives the address of the third element and so forth.

pointers and arrays clip image002 Pointers and Arrays

pointers and arrays clip image004 Pointers and Arrays

Array of pointers

Pointers may also be arrayed like any other data type. To declare an array holding 10 int pointers, the declaration would be as follows:

After this declaration, contiguous memory would be allocated for 10 pointers that can point to integers.

pointers and arrays clip image006 Pointers and Arrays

Now each of the pointers or the elements of pointer array can be initialized. To assign the address of an integer variable amount to the forth element of the pointer array, we will write

pointers and arrays clip image008 Pointers and Arrays

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Daniel Memetic on

Leave a Reply

Related Posts

  • Arrays

    An array is a collection of C or C++ data elements or objects of the same type. Individual elements of an array are referenced using one or more integer indices. A one-dimensional array is defined with a statement such as double vector[50]. This defines (and allocates space for) 50 variables of type double that are [...]...


  • Pointers

    A pointer is a variable that holds the memory address, usually a location of another variable in memory. The use of pointers is critical for the successful programming in C++. The three reasons that support this are, Pointers provide the means through which the memory location of a variable can be directly accessed and hence [...]...


  • Merge Sort

    Merging means combining elements of two arrays to form a new array. The simplest way of merging two arrays is to first copy all the elements of one array into a new array and then append all the elements of the second array to the new array. If you want the resultant array to be [...]...


  • Inserting in a Doubly Linked List

    Inserting an Element To insert an element in the list, the first task is to allocate memory for a new node, assign the element to be inserted to the info field of the node, and then the new node is placed at the appropriate position by adjusting appropriate pointers. Insertion in the list can take [...]...


  • Insertion in a Linear Linked List

    To insert an element in the list, the first task is to create a new node, assign the element to be inserted to the info field of the node, and then place the new node at the appropriate position by adjusting the appropriate pointers. Insertion in the list can take place at the following positions: [...]...