| 251 | | |
| 252 | | |
| 253 | | |
| | 251 | '''Double pointers''': The double pointer can be used for 2 differents things: |
| | 252 | * A pointer that points to a pointer, so that you can change the value of the original pointer (address value) |
| | 253 | * ''To modify the value of a variable using pointers in a function, you need to pass the address of that variable'': Same thing when you try to modify the address of a pointer (pointer of a pointer) |
| | 254 | * A two-dimensional array: like in '''argv''', a matrix, or a list of char*, so (*char[]) |
| | 255 | |
| | 256 | {{{ |
| | 257 | #!C |
| | 258 | /* Pointer to a pointer */ |
| | 259 | void foo( char ** ptr) |
| | 260 | { |
| | 261 | *ptr = malloc(255); // allocate some memory, obtain a new address for ptr |
| | 262 | strcpy( *ptr, "Hello World"); // assign some data to our new addressed pointer |
| | 263 | } |
| | 264 | int main() |
| | 265 | { |
| | 266 | char *ptr = 0; // we set a new pointer to null |
| | 267 | foo( &ptr ); // we call the function with the address (null) of our pointer |
| | 268 | /* now (after to call the function to assign the pointer), we have 'ptr' with a new address and allocated memory */ |
| | 269 | } |
| | 270 | |
| | 271 | int main(int argc, char **argv) /* note that '*argv[]' is equivalent to '**argv' */ |
| | 272 | /* but since it doesn't means the pointer of a pointer, we should use *argv[] in preference because is more understandable to what really means */ |
| | 273 | }}} |