Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Pointers
- Hello, here is how I see the pointers.
- 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:
- int main ()
- {
- int test = 21;
- printf("test is: %d" test);
- }
- But it is also possible to display the address of the variable with the following code:
- int main()
- {
- int test = 21;
- printf("The test address is: %p" test); / * the use of "&" is used to display the test address and not its value * /
- }
- You probably will say "but of what use is the test adress? »
- 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:
- int * pointer = NULL;
- A pointer is initialized by either the value "NULL" or by an address.
- There are now two ways to change the value of the variable "test":
- int main()
- {
- int test = 0;
- int *pointer = &test;
- test = 45;
- printf("test is %d\n", test);
- * Pointer -= 2
- printf("test is %d", test);
- return 0;
- }
- The result of this code in the terminal will be:
- test is 45
- test is 43
- 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!
- 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 ...
- I hope you like this presentation.
- Romain Lebbadi-Breteau
- 0696076393
- r.lebbadibreteau@icloud.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement