Language Elements

Object Oriented Concepts

Data Types

Key Words

Arrays

Pointers

Data Structures

Multithreading

Errors and Exception Handling

Interview Questions



Contact



Arrays in C++


Arrays is an example of structured data types in C++, with elements of same data type placed at the consecutive memory locations.An array can be treated as a single entity, but also allows access to individual members. Each element position in array can individually be referenced by a unique integer index. We can store values of a given type in an array without having to separately declare each variables with a different identifier.

For array declaration, first a type is given for the elements, followed by an identifier, and then integer number of elements within square brackets. For an array of n elements, first array element has index 0 and the last element will have the index n-1. Indexing make it very easy to loop through elements of an array.

An array of five integers can be declaired as:
int array [5] = {0,1,2,3,4};

An array for a string of words can be declaired as:
char someword [] = {'w', 'o', 'r', 'd', '\0' };
or
char someword []="word";
Final null character specifies the end of sequence (appended automatically in the second declaration).

Array can be one dimensional with components in list form (as shown above) or more complex multi-dimensional, with components arranged in tabular form, as matrix of rows and columns.

A two dimensional array of integrs can be declared as:
int array2D [3] [4]={0, 2, 4}{1,5, 7, 8}

This is a 3 x 4 matrix of integers (3 rows and 4 columns), with default value of zero. The value of memory allocation rapidly increases with each additional dimension.



















Copyright © by Zafar Yasin. All rights reserved.