Pointers and One Dimensional Arrays

When an array is declared, the compiler allocates a base address and memory space to store all the elements of the array in contiguous memory locations. The base address is the memory location of the first element of the array. For example, arr is the name of an array containing seven elements and suppose its base address is 1001. Then the memory allocation of the following array declaration will be as shown in figure.

img

Here, in this declaration, the address of the first element of an array can be expressed as either &arr [0] or simple as arr. The address of the second element of the array can be expressed as either &arr [1] or (arr+1), and so on. In general, the address of the (1+1) th element of the array can be expressed as either &arr [1] or as (arr +i). Thus the address of an array element can be represented in following two different ways:

  • • the array element preceded by an ampersand symbol
  • • an expression of adding subscript with array name

The expression (arr+i) is very special an unusual type type of expression. In this expression, are represents the name of the array whereas i is an integer quantity. Moreover, the array elements may be of type char, int, float, double, etc. Thus, it is not a simple arithmetic addition. Rather it is a symbolic representation for specifying the memory location of the (i +1) th element of the array.

Since both &arr[i] and (arr +i) represent the address of the (i+1) th element of array arr, then it can be seem arr [i] and * (arr +i) both represent the value of the (i+1) th element of the array arr. So these two terms can be interchangeably used.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext