You are on page 1of 11

ARRAY

Array About
An array is a series of elements of the same type placed in contiguous memory locations. Individually referenced by an index. type name [size]; A subscript can be an integer expression too, foo[17] foo[i+3] foo[a+b+c]

Types of Array
One Dimensional Arrays
A[0] A[1] A[2] A[3] A[4] A[5]

Two Dimensional Arrays


A[0][0] A[0][1] A[0][2] A[0][3] A[0][4] A[1][0] A[1][1] A[1][2] A[1][3] A[1][4
A[2][1] A[2][2] A[2][3] A[2][3] A[2][4]

Multi Dimensional Arrays 3D Array, 4D Arrays etc.

Declaration of Array
One Dimensional Arrays: int A[10]; Two Dimensional Arrays: char Y[5][25]; Dynamic memory allocation: int * bobby; bobby = new int [5]; delete [] bobby; // Avoid memory Leak

Initialization of the Array


int A[5] = {1,2,3,4,5}; int ARR[ ] = {1,2,3,4,5,6}; //Auto memory allocation char B[20] = Rudrapur ok; // Like a String float C[5] = {2.4,3.5,1.5}; //Partial Initialization possible int D[2][3] = {{1,2,3},{4,5,6}} //{{1,2},{3,4}} Remaining 0 static int a[5]; // static declared to 0; auto =>Garbage.. Memory Allocation with variable const int x=5; // volatile variable not allowed int a[x]; // only const allowed In C++ first element of the array is always at zero position.

Arrays and pass-by-reference


void print_array(int a[], int len) { int j; for(int i=0;i<len;i++){ cout <<"["<<i<<]=<<a[i]<< a[i]+1 <<a[i]+1;} } Inside a function any changes you make to array values are permanent. You need to make sure that a function knows how big the array is!!!

Array Index Out of Bounds


If either the index < 0 or the index > ARRAY_SIZE-1
then we say that the index is out of bounds.

C++ does not check if the index value is within range. Assignment does not work with arrays
A = B // Not Works.. Use for loop instead cout<<A // Not Works.. Use for loop instead

C Strings (Character Arrays)


Character array - an array whose components are of type char. String - a sequence of zero or more characters enclosed in double quote marks.
Char a[25] = Dinesh; cout<<a // valid only for this Contains 7 characters. d i n e s h \0
c_str() // stringVariable.c_str()

Converts a value of the type string to a null-terminated character array

C++ strings
A string is a null terminated array of characters.
null terminated means there is a character at the end of the the array that has the value 0 (null).

Pointers are often used with strings:


char *msg = RPI;
msg

'R'

'P'

'I'

Summary
An array is a structured data type with a fixed number of components Elements of a one-dimensional array are arranged in the form of a list An array index can be any expression that evaluates to a non-negative integer The value of the index must always be less than the size of the array In C++, C-strings are null terminated and are stored in character arrays

Summary (continued)
In a function call statement, when passing an array as an actual parameter, you use only its name As parameters to functions, arrays are passed by reference only A function cannot return a value of the type array

To access an element of a two-dimensional array, you need a pair of indices: one for the row position and one for the column position In row/column processing, a two-dimensional array is processed one row/column at a time

You might also like