Programming

Array In C++…

Array In

C++

 

Array:

An Array is a collection of same type elements in contiguous memory location that can be access by using index of array. Arrays are used to store the data of same datatype. There are arrays of integer, char, double, float, and even of bool. And the most important thing that need to be remember, indexing in C++ is always start from 0.

Syntax:

Datatype arrayName [arraySize];

 

Array of Integers:

Addresses 0x2600 0x2604 0x2608 0x260c 0x2610 0x2614 0x2618 0x261c 0x2620
Values 40 99 61 10 36 12 2 6 52
INDICES 0 1 2 3 4 5 6 7 8

 

The first row shows the address of Array and the second row show the values stored the array. And the last row shows the indexing of array. The size of array is 9. In C++ the index of first element of any array is always 0.

Array of Integers:

Addresses 0x2600 0x2604 0x2608 0x260c 0x2610 0x2614 0x2618 0x261c 0x2620
Values 40 99 61 10 36 12 2 6 52
INDICES 0 1 2 3 4 5 6 7 8

 

The first row shows the address of Array and the second row show the values stored the array. And the last row shows the indexing of array. The size of array is 9.

 

Array of Char:

Addresses 0x1111 0x1112 0x1113 0x1114 0x1115 0x1116 0x1117 0x1118 0x1119
Values P R o T e a C h \0
INDICES 0 1 2 3 4 5 6 7 8

 

In case of character array, compiler add the null in end of array automatically, because the delimiter of char array is null. It’s a convention, the end of the strings in character sequence there is a null. If we make an array of 9 elements of char datatype and store only 3 char elements in it than there is null at third index of array and on remaining indices there is garbage value.

Addresses 0x1111 0x1112 0x1113 0x1114 0x1115 0x1116 0x1117 0x1118 0x1119
Values P R o \0 garbage garbage garbage garbage Garbage
INDICES 0 1 2 3 4 5 6 7 9

 

 

Example1:

Output:

Example2:

Output:

 

Example3:

Output:

 

Example4:

Output:

 

Click to comment

Leave a Reply

Your email address will not be published.

Most Popular

To Top