Array And String

Array

An Array is group similar data of one particular data types, the array's elements are continuous in nature when the array is declared the location of the array elements is continuous in memory.

advantage of the array is we can assess the similar type of data by using only one variable and as the elements of an array are continuous in memory we can assess the element just by knowing the address of the first element we will see that in pointers.

We access the elements of the array by the index in c programming the index of the array starts from 0 to N-1,  where  N is the length of the array.

For example, if we want to access the first element of the array then we can use array[ 0 ],  and array[ 4 ] gives us the fifth element of the array. here the 0 and the 4 is the index for the first and the fifth element of the array.

// image of arrays
Declaration of array:

Declaration of the array is the same as declaring a variable of any other data type, but in the case of an array we need to mention the no of elements in the array in the braces, we can declare an array by the following syntax

data_type  arrayName[size]

Example:

  1. int arr[ 4 ] ;

  2. float array1[ 3 ] ;


The first one is declares the array of integer data type with the size of 4 elements means it can store the 4 data of integer type and the name of the array is arr.

The second one is declares the array of float data type with the size of 3 elements means it can store the 3 data of float type and the name of the array is array1.

When we declare an array, some garbage value is assigned to all the elements and we can assign 0 to all elements at declaration time by using

data_type  arrayName[size] = { 0 } or  data_type  arrayName[size] = {  }.

In the time of declaration array, it is not mandatory to mention the size of the array, it done letter by the compiler when compiling the code, the array is declared according to the values assigned to it at the time. Arrays are static in nature we can't change the size of the array in your program.
Declaration of array with initializing elements:

In this method with declaring the element we also assign values to them. if we don't specify the size of the array in the declaration then the compiler declares array according to values provided in the declaration let's see the examples

1 int a[ 4 ] = { 1,2,3,4 };

It declares an array of four integers and assigns values to them as specified in curly braces means a[ 0 ] to 1, a[ 1 ] to 2 ... a[ 3 ] to 4.

2 int arr[  ] = { 12, 15 , 16 , 40 , 13 , 15 };

It declares an array of 6 integers because six values are specified in braces and assign values to them as specified in curly braces.

3 int arr[ 5 ] = { 1 , 2 }

It declares an array of 5 integers and assigns values to them as specified in curly braces, here the size of arr is 5 and only two values are provided in braces at the declaration in this case the value is assigned to elements as per the no of values provided in the declaration and other elements of array assigned value as 0.

In your example, the first two elements are assigned to values that provided in the declaration, and other elements are assigned as 0. therefor arr is look like the same as [ 1 | 2 | 0 | 0 | 0 ].

int arr[ 3 ] = { 2, 3, 5, 6 }

This declaration gives a warning in c and error in c++, we can't assign no of values more than the size of the array.
Initializing elements of array:

As we have seen earlier that we can initialize the array at the time of the declaration of the array. we can also initialize an array by using the index of the array elements in this method we assign values to the elements of the array individually. let's learn that by the example...

int arr[ 4 ];

// now initializing array elements

arr[ 0 ] = 12;

arr[ 1 ] = 0;

arr [ 2 ] = 43 ;

arr [ 3 ] = arr [ 0 ];
Two-dimensions array

Two-dimension arrays are nothing but the arrays as elements of one array.rather then storing the data if we store the arrays in elements then it becomes 2D array. 2D arrays are also similar to the matrix.
Declaration of 2D array:

Declaration of  2D arrays is quite similar to declaring the one-dimension array, here we also need to specify the size of the second array.

syntax:

data_type name_of_array [ size_of_raw ][ size_of_colomn ];

example:

int  arr[ 2 ] [ 2 ]; it declare 2 X 2, 2-D array.

int arr [ 3 ][ 2 ]; its declare 3 X 2, 2-D array.
Declaration of 2D array with initializing elements:

1 int arr[ 3 ][ 4 ] = {  {0 ,1, 2, 3}, { 4, 5, 6, 7}, { 8, 9, 10, 11} };

This method first declares the 3 X 4 array then assign value to them according to the position.in this case arr[ 0 ][ 0 ] to arr [ 0 ][ 3 ] is assigned as 0,1,2,3 then

arr[ 1 ][ 0 ] to arr [ 1 ][ 3 ] is assigned as 4, 5, 6, 7  and

arr[ 1 ][ 0 ] to arr [ 1 ][ 3 ] is assigned as 8, 9, 10, 11.

we can also diclare an array as below and it done same work as above.

int arr [ 3 ][ 4 ] = {  0 ,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

2 int arr [ 3 ][ 4 ] = {  0 ,1, 2, 3, 4, 5, 6, 7, 8 };

now in this case same as one diamention array the value are assign as per the index an other elements of 2D array is assign as 0.

so in this case first declares the 3 X 4 array then assign value to them according to the position.here

arr[ 0 ][ 0 ] to arr [ 0 ][ 3 ] is assigned as 0,1,2,3 then

arr[ 1 ][ 0 ] to arr [ 1 ][ 3 ] is assigned as 4, 5, 6, 7  and

arr[ 1 ][ 0 ] to arr [ 1 ][ 3 ] is assigned as 8, 0, 0, 0.
Initializing elements of 2d array:

As we initialize one dimension array, we can also initialize a 2D array by the using element's index in an array.

int arr[ 3 ][ 4 ];

arr[ 0 ][ 0 ] = 10;

arr[ 0 ][ 1 ] = 21;

arr[ 0 ][ 2 ] = 7;

arr[ 3 ][ 0 ] = arr[ 0 ][ 1 ] ;

arr[ 3 ][ 1 ] =arr[ 0 ][ 1 ] ;

and so on...
Multidimantion array

we can declare nth dimension array we just need to give that much of sizes in the braces for creating that dimension array.

data_type array_name[ n1 ][ n2 ][ n3 ][ n4 ]...[ nm ];

we can assess and initialize the array as we did in the case of 2D array.

Let's declare a 3D array and initialize it.

int  arr[ 3 ][ 3 ][ 2 ] ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,18}

we can also declare same array as below it is easy to understand.

int  arr[ 3 ][ 3 ][ 2 ] ={

{{1, 2}, {3, 4}, {5, 6}},

{{7, 8}, {9, 10}, {11, 12}},

{{13, 14}, {15, 16}, {17,18}}

}
strings

A string is nothing but an array of characters, the difference between the string and the array of character is the string is terminated by the null character "\0".

we don't need to add the null character at the end of the strings compiler do that for us when the code is compiling.

In string, each character is going to take one byte and one extra byte is required to store the null character. so the size of the string is always going to the length of string + 1 bytes.
Declaration of string:

Declaration of sting is the same as the declaration of an array. but this time the data type is going to char because we are going to store characters inside the string.

char  array_mane[size];

we need to include the null character in the size.

example:

char name[12];

char array[ 5 ];
Declaration of string with initializing elements:

1 char name[15] = { 'M' , 'y' , 'c', 'o', 'l', 'l', 'e', 'g', 'e', 'b', 'o', 'o', 'k', 's'};

2 char name[  ] = { 'M' , 'y' , 'c', 'o', 'l', 'l', 'e', 'g', 'e', 'b', 'o', 'o', 'k', 's'};

3 char name[15] = { "Mycollegebook" };

4 char name[  ] = " mycollegebooks";