CS 107 - 7/27/15 When a varaible declaration is of the form * ; We are creating a "pointer" variable. We are actually storing a memory address in the varaible. The address must be the address of whatever is specified in the varaible declaration. int *var1; var1 will store an address. At the address location, an integer needs to be stored. Pass-by-reference parameters also use addresses, but the compiler is responsible to make sure the code works properly. When using pointers, the programmer must make sure the code works properly. The declaraion of: int * could be a dynamic array of multiple integers or it could be a pointer to a single integer When dealing with a dynamic array, most of the time we just need to treat the array as a normal array. However, any time we need to change the size of the array, we must remember that it is a dynamic array. The following are the lines needed to allocate space for a dynamic array string *dynarr; int daSize = 10; dynarr = new string[daSize]; Often with a dynamic array, we wish to "grow" the array whenever it "fills up". Assume i contains the number of values currently stored in the array, the following code will check when the array is full: // step 1: verify the array is full if ( i >= daSize ) { cout << "Doubling Size of Dyn Arr to " << daSize*2 << endl; // step 2: allocate a new dynamic array of the larger size string *temp; temp = new string [daSize*2]; // step 3: copy the values from the existing array to the // newly created array for (int a = 0 ; a < daSize ; a++) temp[a] = dynarr[a]; // step 4: return the existing back to the operating system delete[] dynarr; // step 5: change the value stored in dynarr to point to // this newly allocated array dynarr = temp; // step 6: update the size variable to reflect the current size daSize = daSize * 2; } The above code has the entire if statement inside of main(). To write the "grow routine" as a method, we would write the following (pay attention to the pass-by-reference parameters). Remember the syntax for a pass-by-reference parameter is: & void increaseSize (string * & dynarr, int & daSize) { cout << daSize << endl; string *temp; temp = new string [daSize*2]; for (int a = 0 ; a < daSize ; a++) temp[a] = dynarr[a]; delete[] dynarr; dynarr = temp; daSize = daSize * 2; cout << daSize << endl; } The code in main() to call this would be: string *dynarr; int daSize; ... if ( i >= daSize ) { cout << "Doubling Size of Dyn Arr to " << daSize*2 << endl; increaseSize ( dynarr, daSize); cout << daSize << endl; } ------------------------------ Records - Chapter 10 An array was a collection of mutliple values of the same type. A record is a collection of multiple values of varying types. In C/C++, the basic form of a record is a "struct" In C++, the advanced form of a record is a "class"/"object". A great spot where records show their usefulness is when we have parallel arrays. From lab 6, we could have created the struct of: struct forecastInfo { string cityName; int highTemp; int lowTemp; string forecast; int foreHigh; int foreLow; }; The above structure declarations normally are written at the top of the source code file outside of any function. forecastInfo cities[400]; forecastInfo city1; // assume the values are read in with the following lines is >> city ; is >> hi >> lo >> fcast >> fhi >> flo; city1.cityName = city; city1.highTemp = hi; city1.lowTemp = lo; city1.forecast = fcast; city1.foreHigh = fhi; city1.foreLow = flo; cities[i].cityName = city; cities[i].highTemp = hi; cities[i].lowTemp = lo; cities[i].forecast = fcast; cities[i].foreHigh = fhi; cities[i].foreLow = flo; At the end of chapter 10, pointers are discussed. This leads to linked lists, which combines structures/records and pointers.