C++ Arrays

Introduction
An Array is a group of variables referenced to by a single name. Array members are called elements and are referenced to by using the index(subscript). The first element in an array has the default index Zero(0). Arrays promote efficiency in programming by reducing the programming code and enhancing memory management.

Array Declaration
An array can be declared and initialized by assigning data to it at the point of declaration,it can be declared and Initialized later or it can be declared and then initialized at the point of data input.

One dimension arrays:
These are arrays whose memory situation is denoted by one row and several columns or one column and several raws Example:

Declaration of a One dimensional array:

Syntax: datatype array name[size];

Example int numbers[3]; char names[5]; double stages[9]; Initializing At the point of declaration: int numbers[3]={10, 15, 25}; In the program: int numbers[3]; numbers[1] = 15; During data input int numbers[3]; cout<<"enter the value"<numbers[2];

Two dimensional arrays:
These are arrays whose memory situation is denoted by two or more raws and columns. The rows form the first dimension and two or more columns form the second dimension.



Syntax: datatype arrayname[rawsize][colsize]; Example: int numbers[4][5]; char items[4][5]; Initializing At the point of declaration: int numbers[4][5]={{10, 15, 25,30, 35},{23, 12, 56, 45, 50},{12, 32, 34, 87, 89},{29, 37, 45, 56, 67}}; In the program: int numbers[4][5]; numbers[1][0] = 23; During data input int numbers[4][5]; cout<<"enter the value"<numbers[2][3];

Three Dimensional
The memory situation is denoted by rows (x-axis), columns (y-axis) and the z-axis giving it the third dimension.



Syntax: datatype arrayname[rawsize][colsize][zsize]; Example: int numbers[3][4][3]; char items[3][4][3];

Dynamic Arrays
These are arrays whose size is not specified and the arrays size acan be determined by the amount of data.

Example: int numbers[]; char names[];

Array Names as Pointers
An array name is a pointer, nothing more. Suppose you have the following array declaration: int ara[6] = {10, 15, 20, 30, 40, 50}; If you printed ara[0], you would see the value 10. If you were to print *ara it would output the value of the first element which is 10 (the first element is ara[0]). An array name is a pointer. Therefore if you print *ara, you also would see 10.The array name, ara, is a pointer pointing to the first element of the array. Therefore, if you dereference that pointer, you dereference the value stored in the first element of the array, which is 10. Dereferencing ara is exactly the same thing as referencing to ara[0], because they both produce the same value (see C++ Pointers)

You can reference an array with subscripts or with pointer dereferencing(using the symbol *). We can use pointer notation to print the third element of ara as in the example below: The following cout prints ara[2] (the third element of ara) without using a subscript: cout << *(ara+2) ; // Outputs ara[2]. The expression *(ara+2) is not vague at all, since an array name is just a pointer that always points to the array’s first element. *(ara+2) takes the address stored in ara, adds two to the address, and dereferences that location. The following is true: ara+0 points to ara[0] // the first element ara+1 points to ara[1] ara+2 points to ara[2] ara+3 points to ara[3] ara+4 points to ara[4] // the fifth element Therefore, to print, store, or perform calculations with an array element, you can either use the subscript notation or the pointer notation.

Character Pointers
The ability to change pointers is best seen when working with character strings in memory. You can store strings in character arrays, or point to them with character pointers. Observe the following two string definitions: char strAra[] = “C++ is fun”; // An array holding a string char *strptr = “C++ is Wow”; // A pointer to the string

Declaring Arrays of Structures
It is easy to declare an array of structures. Specify the number of reserved structures inside array brackets when you declare the structure variable. Consider the example below: struct stores {  int employees; int registers; double sales; } strStore1, strStore2, strStore3, strStore4, strStore5; This structure should not be difficult for you to understand because there are no new commands used in the structure declaration. The structure declaration creates five structure variables. Each structure variable has three members—two integers followed by a double floating-point value. If the second store increased its employee number by six, you could update the store’s employee number by using the following assignment statement: strStore2.employees += 6; // Add three to this store’s employee number.

If fifth store just opened and you want to initialize its members with data. If the stores are a chain and the new store is similar to one of the others, you can initialize the store’s data by assigning each of its members the same data as of another store’s. like this: strStore5 = strStore4; // Defining initial values for the members of store5.

These declarations are fine for a small number of structures, but if the stores were a multinational chain, these five structure variables would not be enough. Suppose there were 2500 stores. You would not want to create 2500 different store variables and work with each one individually. this would be easier to create if you use an array of store structures.

Example 1: struct stores { int employees; int registers; double sales; } store[2500];

Example 2 struct stores { int employees; int registers; double sales; } strStore[];

The data type for strStore[36] is the same for strStore[1981] and strStore[724]. The name of the array, store, is a pointer constant to the starting element of the array, strStore[0]. Therefore, you can use pointer notation to reference the stores. To assign strStore[80] the same value as strStore[28], you can reference the two elements like this: *(strStore+80) = *(strStore+28); You also can mix arrays and pointer notation, in this way--> strStore[80] = *(strStore+28); and receive the same results. You can increase the sales of strStore[8] by 40 percent using pointer or subscript notation as well, as in strStore[8].sales = (*(strStore+8)).sales * 1.40; The extra pair of parentheses are required because the dot operator has precedence over the dereferencing symbol in the C++ hierarchy of operators