SUBSCRIPTED VARIABLES AND POINTERS One-Dimensional Arrays Arrays, Loops, and Functions Multi-Dimensional Arrays Defining and Initializing Pointers Pointers and Arrays Using Pointers as Function Parameters Arrays Array is indexed data structure used to store data elements of same type Can store ints, floats, chars, enumerated data, arrays, pointers, structs, user-defined types Zero-based arrays -- indices are 0,1,2,.... Array of ints (age) 5 11 7 4 0 21 6 2 14 7 [0],[1],[2],[3],[4],[5],[6],[7],[8],[9] age[5] array name is age array index is 5 array element is 21 All elements in array must be same type One-Dimensional Arrays type array_name [number_elements]; int age [10]; float temp [24]; char name [20]; Best to use constants for arrays... const int PEOPLE = 10; const int HOURS = 24; const int NAME_LEN = 20; ... int age [PEOPLE]; float weight [PEOPLE]; float temp [HOURS]; char first_name [NAME_LEN]; char last_name [NAME_LEN]; Accessing Arrays age [i] = 24; weight [i] = pounds; name [k] = 'B'; name = "Robert"; // doesn't work strcpy (name, "Robert"); // instead cin >> temp [j]; Arrays and Loops for (i=0; i row i, column j Arrays and Nested Loops float sales [STATES][CITIES]; for (state=0; state> sales [state][city]; } } Passing a 2D array to a function: prototype: float sumup (float [][], int, int); use total_sales = sumup (sales, STATES, CITIES); Passing a row of a 2D array to a function that expects a 1D array: for (state=0; state single float storage location sales [reg][state] -> 1D array of length CITIES sales [reg] -> 2D array with STATES rows and CITIES columns sales -> refers to entire array No limit on number of dimensions float sales [ZONE][DISTRICT][REGIONS] [STATES][CITIES]; But, remember the sizes multiply 10 x 15 x 20 x 4 x 100 = 1.2 million! Pointers Pointer represents a physical memory address Array Name is actually address of first element in array -- age is address of age [0] -- pointer void increase (int&); Call-by-reference uses a pointer Pointers are powerful, efficient, ... dangerous Defining and Initializing Pointers int *p1; //p1 is a pointer to an integer int x; x = 25; p1 = &x; //p1 is a pointer to x The name of a pointer preceded by a * indicates the contents of the location pointed to... int y; y = *p1; //y is now 25 *p1 = 10; //x is now 10 int *p2; //p2 is a pointer to an integer p2 = &y; //p1 points to x, p2 points to y *p1 = *p2; //put value pointed to by p2 //into location pointed to by p1 p1 = p2; //p1 pointer now has same address as p2 p1 = *p2; //No! p1 is pointer, but *p2 is int *p1 = p2; //No! *p1 is int, but p2 is pointer Pointers can be used to allocate memory dynamically int *p1; //p1 is a pointer to an integer p1 = new int; Integer storage location created -- only access to it is through p1 new operator creates storage location dynamically (when this statement is executed) *p1 = number; mass = *p1 - 5; delete p1; delete operator destroys integer storage location, but p1 is still available to use as a pointer to another int Pointers and Arrays int age [10]; 5,11,7,4,0,21,6,2,14,7 _ [0],[1],[2],[3],[4],[5],[6],[7],[8],[9] *age same as age[0] *(age+1) same as age[1] *(age+k) same as age[k] Arrays of Pointers float *defects[SIZE]; defects[k] = new float; *defects[k] = 127.25; delete defects[k]; Really useful for large data items big_data_structure *defects[SIZE]; Using Pointers as Function Parameters prototype: void swap (int *, int *); function: void swap (int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; } use: int *ptr1; int *ptr2; ... ptr1 = &mass; ptr2 = &media; swap (ptr1, ptr2);