Cas d'Utilisation des Pointers
When you pass a variable to a function, you're essentially passing its value. This means the function receives a copy of the data. Any modifications made inside the function do not affect the original variable.
main.cpp
12345678910#include <iostream> void increment(int num) { num++; } int main() { int num = 5; increment(num); std::cout << "Original value: " << num << std::endl; }
We can use pointers to enable a function to alter the original variable. This involves passing a memory address as an argument instead of the actual value.
main.cpp
123456789101112#include <iostream> void increment(int* num) { (*num)++; } int main() { int num = 5; int* p_num = # increment(p_num); std::cout << "Original value: " << num << std::endl; }
Note
You can bypass the creation of a pointer to a variable and instead directly use the address-of operator when passing a variable.
Swipe to start coding
Write a function that swaps the values of two integer variables using pointers.
- Create a function
swapthat takes two pointers to integers as parameters. - Inside the function, use a temporary variable to store the value of the first variable.
- Assign the value of the second variable to the first variable using pointer dereferencing.
- Assign the value from the temporary variable to the second variable using pointer dereferencing.
- In
main, callswap, passing the addresses of the two variables.
Solution
Merci pour vos commentaires !
single
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Génial!
Completion taux amélioré à 5.88
Cas d'Utilisation des Pointers
Glissez pour afficher le menu
When you pass a variable to a function, you're essentially passing its value. This means the function receives a copy of the data. Any modifications made inside the function do not affect the original variable.
main.cpp
12345678910#include <iostream> void increment(int num) { num++; } int main() { int num = 5; increment(num); std::cout << "Original value: " << num << std::endl; }
We can use pointers to enable a function to alter the original variable. This involves passing a memory address as an argument instead of the actual value.
main.cpp
123456789101112#include <iostream> void increment(int* num) { (*num)++; } int main() { int num = 5; int* p_num = # increment(p_num); std::cout << "Original value: " << num << std::endl; }
Note
You can bypass the creation of a pointer to a variable and instead directly use the address-of operator when passing a variable.
Swipe to start coding
Write a function that swaps the values of two integer variables using pointers.
- Create a function
swapthat takes two pointers to integers as parameters. - Inside the function, use a temporary variable to store the value of the first variable.
- Assign the value of the second variable to the first variable using pointer dereferencing.
- Assign the value from the temporary variable to the second variable using pointer dereferencing.
- In
main, callswap, passing the addresses of the two variables.
Solution
Merci pour vos commentaires !
single