|
| 1 | +# pointers |
| 2 | +A pointer is a variable that contains the address of a variable |
| 3 | +## Pointers and addresses |
| 4 | +`A typical machine has an array of consecutively numbered or addressed memory cells.`<br> |
| 5 | +`&: gives address of an object, this operator can be applied only to objects in memory : variable and array elements.`<br> |
| 6 | +`*: when applied to a pointer, it accesses the object that pointer points to.`<br> |
| 7 | +`Every pointer points to a specific data type` |
| 8 | +```cpp |
| 9 | +int x = 1, y = 2, z[10]; |
| 10 | +int *ip; // pointer to int |
| 11 | +int *iq; // pointer to int |
| 12 | +ip = &x; // ip points to x, since ip stores the address of x |
| 13 | +iq = ip; // iq points to whatever ip pointed to |
| 14 | +y = *ip; // y is now 1 |
| 15 | +*ip = 0; // x is now 0 |
| 16 | +ip = z + 2; // ip points to z[2]; |
| 17 | +*ip = 5; // z[2] is now 5 |
| 18 | +``` |
| 19 | +`if a pointer ptr points to a variable x, then *ptr can occur in any context where x could.` |
| 20 | + |
| 21 | +## Pointers and Function arguments |
| 22 | +```cpp |
| 23 | +void swap(int *x, int *y){ |
| 24 | + int * temp; |
| 25 | + temp = x; |
| 26 | + x = y; |
| 27 | + y = temp; |
| 28 | +} |
| 29 | +int main(){ |
| 30 | + int a = 10, b = 20; |
| 31 | + swap(&a, &b); // pass address |
| 32 | + return 0; |
| 33 | +} |
| 34 | +``` |
| 35 | +
|
| 36 | +## Pointers and arrays |
| 37 | +Any operation that can be achieved by array indexing can also be done with pointers. |
| 38 | +```cpp |
| 39 | +int arr[10]; // a[10]: {1,2,3,4,5,6,7,8,9,0} |
| 40 | +int *pa; // pa pointer to int |
| 41 | +pa = &arr[0]; // pa pointers to a |
| 42 | +pa = arr; // &arr[0] and array-name have identical value |
| 43 | +int x = *pa; // x is no arr[0] |
| 44 | +``` |
| 45 | +`Note: A pointer is a variable so pa = arr and pa++ is legal, but an array name is not a variable so arr = pa or arr++ is illegal.`<br> |
| 46 | +`pass array name as function argument` |
| 47 | + |
| 48 | +```cpp |
| 49 | +void printArray( const int *arr, int n){ |
| 50 | + for(int i=0; i<n; i++){ // sizeof(arr) will return size of arr pointer not size of array. |
| 51 | + // *(arr + i) = 4; // error: read-only variable is not assignable |
| 52 | + printf("%d", *(arr + i)); |
| 53 | + } |
| 54 | +} |
| 55 | +int main(){ |
| 56 | + int arr[] = {0,1,2,3,4,5,6}; |
| 57 | + printArray(arr, 7); |
| 58 | +} |
| 59 | +``` |
| 60 | +
|
| 61 | +```cpp |
| 62 | +// strlen: return length of string s |
| 63 | +int strlen(const char *s){ |
| 64 | + int n = 0; |
| 65 | + while(s!='\0'){ |
| 66 | + n++; |
| 67 | + s++; |
| 68 | + } |
| 69 | + return n; |
| 70 | +} |
| 71 | +``` |
| 72 | +## Pointes arithmetic |
| 73 | +`pointers, arrays, and address arithmetic is one of strengths of the language.`<br> |
| 74 | +`if p is a pointer to some element of an array, then p++ increments p to the next element of the array, and p+=i increments to point ith elements beyond where it currently pointed.`<br> |
| 75 | + |
| 76 | +`The valid pointer operations` : |
| 77 | +1. assignment of pointers of the same type, |
| 78 | +2. adding and subtracting a pointer and integer, |
| 79 | +3. assigning and comparing to zero(NULL).<br> |
| 80 | +4. subtracting and comparing two pointers to members of the same array, |
| 81 | + |
| 82 | +`Illegal pointer operations` : |
| 83 | +1. All other pointer operation are illegal, |
| 84 | +2. not legal to add/subtract/multiply/divide/shift/mask two pointers. |
| 85 | +3. add float or double to them. |
| 86 | +4. Assign a pointer of one type to a pointer of another type without a cast illegal. except `void *` |
| 87 | + |
| 88 | +## Character pointers and functions |
| 89 | +A string constant, written as |
| 90 | +```cpp |
| 91 | +"I am a string" |
| 92 | +``` |
| 93 | +is an array of characters. In the internal representation, the array is terminated with null character `\0` so that program can the find end. |
| 94 | + |
| 95 | +```cpp |
| 96 | +char amessage[] = "now is time"; // amessage: [now is time\0] |
| 97 | +char *pmessage = "now is time"; // pmessage --> [now is time\0] |
| 98 | +``` |
| 99 | + |
| 100 | +## pointer array |
| 101 | +Array of pointers. |
| 102 | +```cpp |
| 103 | +/* |
| 104 | +int *p1, *p2; |
| 105 | +var: [p1, p2]; |
| 106 | +*/ |
| 107 | +int *var[2]; // var is array of size 2 pointer to integer |
| 108 | +/* |
| 109 | +var -> [1,2] |
| 110 | +*/ |
| 111 | +int (*var)[2] // var is pointer to array of size 2 integer. |
| 112 | +``` |
| 113 | + |
| 114 | +## 2-D arrays |
| 115 | +If 2D array is to be passed to a function, parameter declaration in function must include the number of columnss the number of rows is irrelevant, since a pointer to an array of row is passed to a the function. |
| 116 | +```cpp |
| 117 | +// Online C++ compiler to run C++ program online |
| 118 | +#include <iostream> |
| 119 | +/*void show2D(int dayTab[4][5]){..} |
| 120 | +void show2D(int dayTab[][5]){...}*/ |
| 121 | +void show2D(int (*dayTab)[5]){ |
| 122 | + for(int i =0; i < 4; i++){ |
| 123 | + for(int j = 0; j < 5; j++){ |
| 124 | + std::cout << dayTab[i][j] << "\t"; |
| 125 | + } |
| 126 | + std::cout <<"\n"; |
| 127 | + } |
| 128 | +} |
| 129 | +int main() { |
| 130 | + int dayTab[4][5] = {{0,1,2,3,4}, |
| 131 | + {0,1,2,3,4}, |
| 132 | + {0,1,2,3,4}, |
| 133 | + {0,1,2,3,4} |
| 134 | + }; |
| 135 | + show2D(dayTab); |
| 136 | + return 0; |
| 137 | +} |
| 138 | +``` |
| 139 | +
|
| 140 | +## Pointers to functions |
| 141 | +A function itself is not a variable, but it is possible to define pointers to functions which can be assigned, placed in arrays, passed to functions, returned by functions and so on. |
| 142 | +
|
| 143 | +```cpp |
| 144 | +// function pointer |
| 145 | +#include<iostream> |
| 146 | +using namespace std; |
| 147 | +typedef bool (*funPtr)(int, int); |
| 148 | +typedef int *p; |
| 149 | +bool fun(int x, int y){ |
| 150 | + return x>y?true:false; |
| 151 | +} |
| 152 | +void fun1(int x, int y, bool (*fun2)(int, int)){ |
| 153 | + |
| 154 | + cout << boolalpha << fun2(x,y) << endl; |
| 155 | +} |
| 156 | +funPtr fun3(){ |
| 157 | + return fun; |
| 158 | +} |
| 159 | +bool (*fun4())(int, int){ |
| 160 | + return fun; |
| 161 | +} |
| 162 | +int main(){ |
| 163 | + cout << boolalpha << fun(4,3) << endl; |
| 164 | + bool (*comp)(int, int); |
| 165 | + comp = fun; |
| 166 | + cout << boolalpha << comp(4,3) << endl; |
| 167 | + fun1(4,6,fun); |
| 168 | + funPtr f = fun3(); |
| 169 | + cout<< boolalpha << f(4,5) << endl; |
| 170 | + funPtr arr[2] = {comp, f}; |
| 171 | + cout << boolalpha << arr[0](4,5) << endl; |
| 172 | + cout << boolalpha << arr[0](7,5) << endl; |
| 173 | + bool(*fun5)(int, int) = fun4(); |
| 174 | + cout << boolalpha << fun5(6,7) << endl; |
| 175 | + p q; |
| 176 | + q = new int(2); |
| 177 | + cout << *q; |
| 178 | +} |
| 179 | +/* |
| 180 | +true |
| 181 | +true |
| 182 | +false |
| 183 | +false |
| 184 | +false |
| 185 | +true |
| 186 | +false |
| 187 | +2 |
| 188 | +*/ |
| 189 | +``` |
| 190 | + |
| 191 | +## Complicated Pointer Declarations |
| 192 | +`right -> left rules`:<br> |
| 193 | +First, symbols. Read<br> |
| 194 | +- `*` as "pointer to" |
| 195 | +- `[]` as "array of" |
| 196 | +- `()` as "function returning" |
| 197 | + |
| 198 | +1. STEP1: |
| 199 | +Find the identifier. This is your starting point. |
| 200 | +then say `Identifier is ...` |
| 201 | +2. STEP2: Look at the symbols on the right of the identifier. continue right until you run out of symbols *OR* hit a *right* parenthesis `)`. |
| 202 | +3. STEP3: Look at the symbols to the left of the identifier. If it is not one of our symbols just say it. otherwise translate into English using above table. keep going left until you run out of symbols *OR* hit a *left* parenthesis `(`. |
| 203 | + |
| 204 | +Example: |
| 205 | +int *p[]; |
| 206 | +1. find identifier "p is" |
| 207 | +2. move right until run out of symbols or right parenthesis hit. |
| 208 | + "p is array of" |
| 209 | +3. can't move right anymore so move left and find. |
| 210 | + "p is array of pointer to" |
| 211 | +4. keep going left until you run out of symbols or left parenthesis hit |
| 212 | + "P is array of pointer to int." |
| 213 | +<br> |
| 214 | +`int*(*func())()` |
| 215 | +func is function returning pointer to function returning pointer to int. |
| 216 | +<br> |
| 217 | +`int (*(*fun_one)(char *,double))[9][20];` |
| 218 | +fun_one is pointer to function(char *, double) returning pointer to array(size 9) of array(size 20) of int. |
| 219 | + |
| 220 | +## typedef specifier used in declaration |
| 221 | +`Typedef can be used to aliasing predefined types.` |
| 222 | + |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + |
0 commit comments