/* ************************************************************************** * Program name : 050_what_is_pointer (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/07/01 - first version * ************************************************************************** * Description : (a) Input an integer number (1-10) * * (b) Output the memory address and the value of Number * * (c) Input the values of all element in the array Values * * (d) Output the memory address and elements of Values * * (e) Try again ? * ************************************************************************** */ #include #include int main() { // part 1 : declaration int Number; int *NumPointer; // Note (1) float Values[10]; float *ValuesPointer; int Index; char Again; do { do { // part 2 : Input an integer number (1-10) cout << "\n\nPlease input an integer number (1-10) : "; cin >> Number; NumPointer = &Number; // Note (2) // part 3 : Output the memory address and value of variables cout << "\n\tVariables \tMemory address\tContains" << "\n\t---------------\t--------------\t------------" << "\n\tNumPointer\t" << &NumPointer << "\t" << NumPointer << "\n\tNumber\t\t" << &Number << "\t" << *NumPointer << "\n\t---------------\t--------------\t------------\n\n"; } while (Number<1 || Number>10); system("PAUSE"); // part 4 : Input the values of all element in the array Values ValuesPointer = Values; // Note (3) cout << "\n\n\tPlease input " << Number << " real numbers : "; for (Index=0; Index<=Number-1; Index++) { cout << "\n\tReal numbers [" << Index+1 << "] :"; cin >> ValuesPointer[Index]; // same as "cin >> Values[Index];" } // part 5 : Output the memory address and all elements of array Values cout << "\n\n\tMemory addresses:\tValues: "; for (Index=0; Index<=Number-1; Index++) { cout << "\n\n\t" << ValuesPointer << "\t\t" << *ValuesPointer; ValuesPointer++; // Note (4) } // part 6 : try another number ? cout << "\n\n\tTry another number (Y/N) : "; cin >> Again; cout << "\n"; } while (Again=='Y' || Again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; } /* Notes (1) A pointer is a variable that holds a memory address. Most comonly this address is the location of another variable in memory. A pointer declaration consists of a basic type , an *, and the variable name. syntex : type *name example : int *NumPointer (2) (a) There are two pointer operators. Operator |Sample |Function ---------|----------------------|---------------------------------------- & |&Number |Returns the memory address of the | |variable Number ---------|----------------------|---------------------------------------- * |*NumPointer |Returns the value of the variable locate | |at the address as stated by NumPointer ---------|----------------------|---------------------------------------- (b) Since the pointer Numpointer is assigned to the memory location of the variable Number, therefore the result may be (if Number == 3): Variables Memory address Contains --------------- ----------------------- ------------------------- NumPointer 0x254fda0 (&NumPointer) 0x254fda4 (NumPointer) Number 0x254fda4 (&Number) 3 (*NumPointer or Number) --------------- ----------------------- ------------------------- (3) The pointer ValuesPointer is assigned to the memory location of the first element in the array Values (since refer to an array, so does not use &). For example : Memory addresses: Values: 0x254fdb8 12.3 (4) Each time ValuePointer is incremented, it will point to the memory location of the next element in the array Values. For example : Memory addresses: Values: 0x254fdb8 12.3 0x254fdbc -6.356 0x254fdc0 2.979 */