Professional Documents
Culture Documents
Computer
Programming
● Array Definition
● Array referencing
Chapter 5 ● One dimensional and
Arrays and multidimensional
String arrays
● Strings: Definition,
Manipulation accessing Strings
● An array is a series of elements of the same
type placed in contiguous memory locations
● Individual elements can be referenced by an
index to a unique identifier.
● For example, five values of type int can be
declared as an array without having to declare
Array Definition 5 different variables (each with its own
identifier).
● Using an array, the five int values are stored in
contiguous memory locations, and all five can
be accessed using the same identifier, with the
proper index.
● Like a regular variable, an array must be
declared before it is used.
● A typical declaration for an array in C++ is:
Here,
● int - type of element to be stored
● age - name of the array
● 5 - size of the array
4
Few Things to Remember:
int x[6]
6
C++ Array With Empty Members
7
● The values of any of the elements in an array
can be accessed just like the value of a regular
variable of the same type.
● The syntax is:
name[index]
Accessing the ● Following the previous examples in which age
had 5 elements and each of those elements
value of an Array was of type int, we can access the value as the
following example :-
○ For example, the following statement
stores the value 75 in the third element of
age:
age [2] = 75;
○ and, for example, the following copies the
value of the third element of age to a
variable called x:
x = age[2];
8
● The base address of an array is the address
(that is, the memory location) of the first
array component.
● For example, if list is a one-dimensional
array, then the base address of
Memory Address of an Array list is the address of the component list[0].
Consider the following statements:
int myList[5];
11
● For example, the way to reference the
second element vertically and fourth
horizontally in an expression would be:
○ Period [1][2]
Multidimensional ● Note:-
(remember that array indices always
arrays begin with zero).
● Multidimensional arrays are not limited
to two indices (i.e., two dimensions).
● They can contain as many indices as
needed.
● For example:
char century [100][365][24][60][60];
12
⚫ At some point, we may need to pass an
array to a function as a parameter.
⚫ In C++, it is not possible to pass the entire
block of memory represented by an array
to a function directly as an argument. But
what can be passed instead is its address. In
Arrays as practice, this has almost the same effect,
and it is a much faster and more efficient
parameters operation.
⚫ To accept an array as parameter for a
function, the parameters can be declared as
the array type, but with empty brackets,
omitting the actual size of the array. For
example:
void procedure (int arg[])
This function accepts a parameter of type
"array of int" called arg. 13
Strings
● is a collection of characters.
For example
16
● Character sequences
○ char name [20];
○ For example, either the sequence "Hello" or
the sequence "Merry Christmas" can be
stored in name, since both would fit in a
sequence with a capacity for 20 characters.
Strings: Definition, ○ By convention, the end of strings represented
in character sequences is signaled by a special
accessing Strings character: the null character, whose literal
value can be written as '\0' (backslash, zero).
○ Because arrays of characters are ordinary
arrays, they follow the same rules as these.
○ For example, to initialize an array of
characters with some predetermined
sequence of characters, we can do it just like
any other array:
char myword[ ] = { 'H', 'e', 'l', 'l', 'o', '\0' };
17
⚫ Character sequences
◦ In fact, because string literals are regular
arrays, they have the same restrictions
as these, and cannot be assigned values.
◦ Expressions (once myword has already
been declared as above), such as:
Strings: Definition, myword = "Bye";
myword[ ] = "Bye";