Advertisement
Carlos97232

The pointers - English version

Mar 30th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. Pointers
  2.  
  3. Hello, here is how I see the pointers.
  4.  
  5. As you know, each variable has a value and an address, when we define a variable, the OS looks for a free address and then gives it to the program. When we want to display the value of the variable, we just write:
  6. int main ()
  7. {
  8. int test = 21;
  9. printf("test is: %d" test);
  10. }
  11.  
  12. But it is also possible to display the address of the variable with the following code:
  13. int main()
  14. {
  15. int test = 21;
  16. printf("The test address is: %p" test); / * the use of "&" is used to display the test address and not its value * /
  17. }
  18.  
  19. You probably will say "but of what use is the test adress? »
  20. For example, to return many values ​​from a function, it is possible to store the variable address in a variable called type "pointer", these variables are defined as follows:
  21. int * pointer = NULL;
  22. A pointer is initialized by either the value "NULL" or by an address.
  23. There are now two ways to change the value of the variable "test":
  24. int main()
  25. {
  26. int test = 0;
  27. int *pointer = &test;
  28.  
  29. test = 45;
  30. printf("test is %d\n", test);
  31.  
  32. * Pointer -= 2
  33. printf("test is %d", test);
  34. return 0;
  35. }
  36.  
  37. The result of this code in the terminal will be:
  38.  
  39. test is 45
  40. test is 43
  41.  
  42. Indeed, one can modify a variable by changing the pointer of the variable preceded by "*". Now you know how to change multiple variables with a single function!
  43. You will for example need pointers to save text in files, to allow the user to type his first name, to create an identity card ...
  44. I hope you like this presentation.
  45.  
  46. Romain Lebbadi-Breteau
  47. 0696076393
  48. r.lebbadibreteau@icloud.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement