/* This program demonstrates: * 1. pointers. * 2. null pointers * 3. Reinterpret casts * 4. Pass by address * 5. Arrays and Pointers - pointer arithmetic * * Pointers are variables that can hold the address of another variable. * * Pointer Arithmetic * Since pointers are numbers (integers) we can do math with them. * However, the second operand in the operation is always multiplied by the size * of the type of the variable before the math is done. * We can make use of this property to work with arrays * * Pointers and arrays * When we declare an array, we are actually declaring a contiguous space of the * required amount of memory, and then uses the name os the array as a pointer to * the starting address of the array * Every time we use the [] operator with the array, we are actually doing pointer * arithmetic and then dereferencing the pointer * So, we can actually do the pointer math and dereference the pointer to move * through the array. * We can also actually change and update the pointer to move through the array. * However, if we do this, we have to make a note of the fact that the array name * is no longer pointing at the starting address of the array */ #include using namespace std; void swap (int, int); void swapRef( int &, int &); void swapAddress(int *, int *); int main() { /* A variable has 5 properties: type, size, name, value and address * We can store the address of a variable in another variable. * This new variable that contains the address of another variable is called a pointer * We declare pointer using the * operator. * We get the address of a variable using the & operator. */ int var = 50; // regular integer variable int *ptr; // declare a pointer ptr = &var; // Initialize it with teh address of x cout<<"var: "<< var < null pointer exception - will crash // We cannot assign pointers to integer literals //ptr = 0xffffcc3c; // error /* Valid R-values for pointer assignment: * 1. Address of variables of the same type * 2. null * 3. pointer of the same type * 4. reinterpret cast */ // We can assign a pointer to another pointer o the same type int *p2; int anothervar = 15; p2 = &anothervar; ptr = p2; cout<<"After re-assignment, *ptr: "<< *ptr<(dblPtr); cout<<"After reinterpret cast: ptr: "<>a>>b; //Swapping 3 ways - swap by vallue, swap by reference and swap by address cout<<"Pass by value:\n"; cout<<"In main, before swap:\nA = "< ptr op (type_size * x) * * The following loop runs 10 times, each time it dereference the pointer * after adding i to the pointer * Since it uses pointer arithmetic, the actual value added to the pointer * is i * sizeof(int) = i*4 here */ cout<<"\nPrinting array, with pointer arithmetic:\n"; for(int i=0; i<10; i++) cout<< *(arrPtr +i) <<"\t"; /* The following loop does the same thing as the previous loop, but it makes use of operator precedence * to combine the addition and dereferencing into one step. * It also has the effect of changing the pointer. * At the end of the previous loop, the pointer will still be pointing at element number 0 of the array. * At the end of the following loop, the pointer will be pointing at the spaces beyond the last element * (element 9) of the array. */ cout<<"\nPrinting array by changing the pointer:\n"; for(int i=0; i<10; i++) { cout< arrPtr = arrPtr + (4*1) } cout<<"After the loop: arrPtr: "<