Professional Documents
Culture Documents
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]
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
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
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).
'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