Arrays in C++
Arrays are among the most basic data structures in computer science. An array is simply, a data storing entity that stores homogenous data values.
For example, if we need to store a sequence of integer values such as 3, 5, 7, 9 and 11 then rather than declaring 5 integer variables, we can use 1 integer array to store all the five values.
location 0, accessed by array[0] contains the value 3
location 1, accessed by array[1] contains the value 5
location 2, accessed by array[2] contains the value 7
location 3, accessed by array[3] contains the value 9
location 4, accessed by array[4] contains the value 11
The int before array, in array declaration describes the type of data that the array can store. It can be any valid data type such as int, string, char, float, etc.
Arrays can be single dimensional or multi dimensional. The array used in the example above is single dimensional.
A two-dimensional array with 2 rows and 3 columns can be declared as :
The values are stored as in a matrix.
1 2 3
2 4 8
array[0][0] contains the value 1
array[0][1] contains the value 2
array[0][2] contains the value 3
array[1][0] contains the value 2
array[1][1] contains the value 4
array[1][2] contains the value 8
Format for a declaring n dimensional array :
Arrays consist of contiguous memory locations. Therefore, we can use the memory address of array[0] to reach memory addresses of other variables stored in the same array.
If you do not know about pointers yet, the following part may not make sense to you. Skip it, but make sure to read about pointers as they are very important.
When we declare an array as :
Here, names is actually a constant pointer to the address of the first element stored in the array. You can access the values stored in the array as :
*(names+0) will give the value "Dexter" : same as names[0]
*(names+1) will give the value "Scythe" : same as names[1]
*(names+2) will give the value "Macbox" : same as names[2]
If you declare another pointer :
*(p+0) will give the value "Dexter"
*(p+1) will give the value "Scythe"
*(p+2) will give the value "Macbox"
In C++, you cannot pass an entire array as an argument to a function. Rather, you have to pass a pointer to the array without any index value.
You would have to declare the function as one of the three ways :
Each tells the compiler that an integer pointer is going to be received as the parameter.
Similarly, to return an array from a function, you have to return an pointer to an array:
Example program :
The function myfunction modifies the first element in the passed array and returns a pointer to the modified array.
Output :
Arrays are stored in contiguous memory, so if you create an array of size 10 and allocate array[11] and array[12] then if the memory after array[10] is unallocated then they will be stored at that address. If afterwards, any object is assigned that memory location then array[11] and array[12] will get corrupted.
For example, if we need to store a sequence of integer values such as 3, 5, 7, 9 and 11 then rather than declaring 5 integer variables, we can use 1 integer array to store all the five values.
int array[] = {3, 5, 7, 9, 11};
location 0, accessed by array[0] contains the value 3
location 1, accessed by array[1] contains the value 5
location 2, accessed by array[2] contains the value 7
location 3, accessed by array[3] contains the value 9
location 4, accessed by array[4] contains the value 11
The int before array, in array declaration describes the type of data that the array can store. It can be any valid data type such as int, string, char, float, etc.
Arrays can be single dimensional or multi dimensional. The array used in the example above is single dimensional.
A two-dimensional array with 2 rows and 3 columns can be declared as :
int array[2][3] = { {1,2,3} , {2,4,8} };
The values are stored as in a matrix.
1 2 3
2 4 8
array[0][0] contains the value 1
array[0][1] contains the value 2
array[0][2] contains the value 3
array[1][0] contains the value 2
array[1][1] contains the value 4
array[1][2] contains the value 8
Format for a declaring n dimensional array :
type name[size1][size2]...[sizeN];
Arrays consist of contiguous memory locations. Therefore, we can use the memory address of array[0] to reach memory addresses of other variables stored in the same array.
If you do not know about pointers yet, the following part may not make sense to you. Skip it, but make sure to read about pointers as they are very important.
When we declare an array as :
string names[] = { "Dexter" , "Scythe" , "Macbox" };
Here, names is actually a constant pointer to the address of the first element stored in the array. You can access the values stored in the array as :
*(names+0) will give the value "Dexter" : same as names[0]
*(names+1) will give the value "Scythe" : same as names[1]
*(names+2) will give the value "Macbox" : same as names[2]
If you declare another pointer :
string * p;
p = names;
*(p+0) will give the value "Dexter"
*(p+1) will give the value "Scythe"
*(p+2) will give the value "Macbox"
In C++, you cannot pass an entire array as an argument to a function. Rather, you have to pass a pointer to the array without any index value.
int main() {
int array1[] = { 1, 2, 3, 4 };
myfunction (array1) ;
return 1;
}
You would have to declare the function as one of the three ways :
void myfunction(int p[]) { }
void myfunction(int *p) { }
void myfunction(int p[4]) { }
Each tells the compiler that an integer pointer is going to be received as the parameter.
Similarly, to return an array from a function, you have to return an pointer to an array:
int * myfunction() { }
Example program :
#include <iostream>
using namespace std;
int * myfunction(int *p)
{
p[0] = 10;
return p;
}
int main()
{
int array[] = { 1,2,3,4,5 };
int * p2;
p2 = myfunction(array);
for(int i = 0; i < 5; i++)
{
cout<<"*(p2 + "<<i<<") : "<<*(p2+i)<<endl;
}
return 1;
}
The function myfunction modifies the first element in the passed array and returns a pointer to the modified array.
Output :
*(p2 + 0) : 10
*(p2 + 1) : 2
*(p2 + 2) : 3
*(p2 + 3) : 4
*(p2 + 4) : 5Arrays are stored in contiguous memory, so if you create an array of size 10 and allocate array[11] and array[12] then if the memory after array[10] is unallocated then they will be stored at that address. If afterwards, any object is assigned that memory location then array[11] and array[12] will get corrupted.
Comments
Post a Comment