|
| 1 | +#include <iostream> |
| 2 | +using namespace std; |
| 3 | +// A type defined in terms of other types |
| 4 | + |
| 5 | +// Simple function to demonstrate function pointers |
| 6 | +int foo(int x) |
| 7 | +{ |
| 8 | + cout << "x = " << x << endl; |
| 9 | + return x; |
| 10 | +} |
| 11 | + |
| 12 | +void byPtr(int *x) |
| 13 | +{ |
| 14 | + (*x)++; |
| 15 | + cout << "x = " << *x << endl; |
| 16 | +} |
| 17 | + |
| 18 | +void byConstPtr(const int *const x) |
| 19 | +{ |
| 20 | + // *x++; // error |
| 21 | + cout << "x = " << *x << endl; |
| 22 | +} |
| 23 | + |
| 24 | +void byPtrConst(const int *x) |
| 25 | +{ |
| 26 | + // *x++; // error |
| 27 | + cout << "x = " << *x << endl; |
| 28 | +} |
| 29 | + |
| 30 | +void byConstPtrConst(const int *const x) |
| 31 | +{ |
| 32 | + // *x++; // error |
| 33 | + cout << "x = " << *x << endl; |
| 34 | +} |
| 35 | + |
| 36 | +static int global = 42; |
| 37 | +int *returnPtr() |
| 38 | +{ |
| 39 | + return &global; // the object must outlive the reference. |
| 40 | +} |
| 41 | + |
| 42 | +void pointers() |
| 43 | +{ |
| 44 | + cout << "\n--- Pointers Type Examples ---\n"; |
| 45 | + int a = 10; |
| 46 | + cout << "a = " << a << "\n"; |
| 47 | + |
| 48 | + // * 1. Pointer basics |
| 49 | + const int *null_ptr = nullptr; |
| 50 | + cout << "Address of nullptr (null_ptr): " << null_ptr << "\n"; |
| 51 | + |
| 52 | + // '&' gives the address of a variable |
| 53 | + int *ptr_a = &a; |
| 54 | + cout << "Address of a (&a): " << &a << "\n"; |
| 55 | + cout << "Value stored in pointer (ptr_a): " << ptr_a << "\n"; |
| 56 | + |
| 57 | + // '*' dereferences a pointer (accesses the value at that address) |
| 58 | + cout << "Value of a via *ptr_a: " << *ptr_a << "\n"; |
| 59 | + |
| 60 | + // Change value of a through its pointer |
| 61 | + *ptr_a = 2; |
| 62 | + cout << "Value of a after *ptr_a = 2: " << a << "\n"; |
| 63 | + |
| 64 | + // * 2. Pointer to const |
| 65 | + [[maybe_unused]] const int const_var = 100; |
| 66 | + const int *ptr_const_var = &const_var; |
| 67 | + // *ptr_const_var = 10; // cannot modify the value through pointer |
| 68 | + cout << "Value of ptr_const_var " << *ptr_const_var << "\n"; |
| 69 | + ptr_const_var = &a; // can point somewhere else |
| 70 | + cout << "Value of ptr_const_var " << *ptr_const_var << "\n"; |
| 71 | + |
| 72 | + // * 3. Const pointer |
| 73 | + int *const const_ptr_a = &a; |
| 74 | + *const_ptr_a = 10; // can change value |
| 75 | + // const_ptr_a = nullptr; //cannot point to another variable |
| 76 | + |
| 77 | + // * 4. Const pointer to const |
| 78 | + [[maybe_unused]] const int *const const_ptr_const_var = &a; |
| 79 | + // *const_ptr_const_var = 10; // cannot modify the value through pointer |
| 80 | + // const_ptr_const_var = nullptr; //cannot point to another variable |
| 81 | + |
| 82 | + // * 5. Pointer to pointer |
| 83 | + int **ptr_ptr_a = new int *[10]; // dynamically allocate an array of 10 int* |
| 84 | + ptr_ptr_a[0] = &a; |
| 85 | + cout << "Value via pointer-to-pointer (*ptr_ptr_a[0]): " << *ptr_ptr_a[0] << "\n"; |
| 86 | + delete[] ptr_ptr_a; // always free heap memory |
| 87 | + |
| 88 | + // * 6. Void pointer (generic pointer) |
| 89 | + // void *void_ptr = static_cast<int*>(&a); |
| 90 | + void *void_ptr = &a; // C-style pointer casting [cstyleCast] |
| 91 | + |
| 92 | + cout << "Value via void pointer (after casting): " << *static_cast<int *>(void_ptr) << "\n"; |
| 93 | + |
| 94 | + // * 7. Function pointer |
| 95 | + int (*fcn_ptr)(int) = &foo; |
| 96 | + (*fcn_ptr)(5); // call via dereference |
| 97 | + fcn_ptr(10); // call directly |
| 98 | + |
| 99 | + // 8. Passing pointers |
| 100 | + int in = 10; |
| 101 | + byPtr(&in); |
| 102 | + byConstPtr(&in); |
| 103 | + byPtrConst(&in); |
| 104 | + byConstPtrConst(&in); |
| 105 | + |
| 106 | + // * 9. Return by ptr(address) |
| 107 | + const int *b_ptr = returnPtr(); |
| 108 | + std::cout << "By ptr: " << *b_ptr << '\n'; |
| 109 | +} |
| 110 | + |
| 111 | +// A struct that runs code when its object is created |
| 112 | +struct CPointers |
| 113 | +{ |
| 114 | + CPointers() |
| 115 | + { |
| 116 | + cout << "\n" |
| 117 | + << "\n" |
| 118 | + << "Compound type: Pointers\n"; |
| 119 | + |
| 120 | + pointers(); |
| 121 | + } |
| 122 | +}; |
| 123 | + |
| 124 | +// All global and static objects are constructed before main() begins. |
| 125 | +static CPointers autoRunInstance; |
0 commit comments