Check The Programming Section
Let’s first declare and initialise an one-dimenstional array num with 10 elements
int num[] = {-3, 5, 12, 89, 91, 94, 23, 34, 45, 76};
Memory Representation of an array with elemets, address and indexes
Like other variables and constants an array also required memory blocks to store the elements. An array is a collection of similar elements, so it requires contigous memory blocks. Look at the image depicted above, at the bottom of the image the addresses of each elements are displayed. Now let’s understand the addressing and indexing of the num array.
The type of num is int, so each elements of the num array will take 4 bytes of memory. For example, consider the starting address of first memory block is 2000. So the next elements of the array will get next contiguous 4 bytes memory block and the memory address will be 2004 (as 2000 + 4 bytes) and so on for other elements. In the above figure, the addresses are displayed here, are the starting address of each memory block assigned to individual element of the array.
4 bytes memory block assigned to each element
We can print the address of the first element by passing the location with name of the array and the required syntax is
cout<<&num[0];
Let’s print the address of first (num[0]) and second element (num[1]) as decimal number and the required code is (edit and try)
#include<cstdio>
using namespace std;
int main(){
int num[] = {-3, 5, 12, 89, 91, 94, 23, 34, 45, 76};
printf("%d %d",&num[0], &num[1]);
}
The output is (may be different in your computer) 6553072 6553076 and the difference with respect to bytes is (6553076 - 6553072 = 4 btyes). The symbol & before name of the first element (num[0]) of the array is the address of operator. As of now just consider [0] is the index of first element of the array. Next, I have discussed about it, just continue your reading. Like this, we can print the address of all the elements by using the required index for a particular element with a loop. The required code is (edit and try)
#include<iostream>
using namespace std;
int main(){
int num[] = {-3, 5, 12, 89, 91, 94, 23, 34, 45, 76};
int size = sizeof num/sizeof num[0];
for(int i=0;i<size; i++){
cout<<"Address of num["<<i<<"] is "<<&num[i]<<endl;
}
}
In the above code (written using C++11 standards), the for loop is used to pass the required index inside the square brackets [ ] and the loop variable i is used for this purpose.
Similarly, we can just use the name of the array num to print the starting address or the base address of the array, which is same as the address of num[0]. The required syntax is
cout<<num;
Indexing is a process to get the location of an element in the array. So, when we write num[0], it informs the compiler about the location of the first element. Few points related to indexing we need to careful in C and C++:
In C/C++, array indexing is started from 0 for the first element. So, 0 will be passed with subsscripting operator [ ], to access the first element of the array.
num[0] = 9;//it updates the value of first element
An index value must be greater than or equal to 0
num[-1] = 4;//an illegal; statement and raised to run-time error
The highest index of an array is equal to size - 1, size is the number of elements in the array.
Before accessing the last element of the array, we must know the number of elelments present in the array. The required code (follows C++11 standards) to calculate the size of the array is
#include<iostream>
using namespace std;
int main(){
int num[] = {-3, 5, 12, 89, 91, 94, 23, 34, 45, 76};
int size = sizeof num/sizeof num[0]; //calculates size of the array
cout<<"The last element of the array is "<<num[size-1];
}
During indexing we can interchange the position 3 and the array name num to access the 4th element of the array. The required syntax is
3[num] = 56; //the item at index 3 get updated
To access an item, we must pass an index inside the subscript operator [ ] by reducing it by 1. Suppose, we wants to access the 5th element, so the required index will be 5-1 and the required code is (edit and try):
#include<iostream>
using namespace std;
int main(){
int num[] = {-3, 5, 12, 89, 91, 94, 23, 34, 45, 76};
int index;
cout<<"Which item you wants to access ";
cin>>index;
cout<<"The item at index "<<index-1<<" is "<<num[index-1];
}