Printing Addresses and Accessing Elements of an Array
Explore different methods of printing addresses and accessing elements of an array.
Printing the address of array elements
There are two ways to print the address of array elements:
- Using the ampersand operator
- Using simple arithmetic
Using the ampersand operator
We can print the addresses of array elements using the expressions: &a[ 0 ], &a[ 1 ], &a[ 2 ], etc.
Using simple arithmetic
Since the array variable name is a pointer, pointing to the element of an array, mentioning the name of an array always fetches its base address, starting address, or the address of its element. Therefore, the addresses of array elements can also be printed using the expressions: a, a + 1, a + 2, etc.
Incrementing the pointer results in the pointer pointing to the next immediate location of its type. Hence, a will give us the address of the element, and a+1 will give us the address of the element.
Accessing elements of an array
We can access array elements using the expressions: subscript notation, a[ i ], or pointer notation, *( a + i ).
Note: Since every
a[ i ]gets expanded to*( a + i ), the expression,a[ i ], is the same asi[ a ].