Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Pointers Use Cases | Pointers Fundamentals
C++ Pointers and References

bookPointers Use Cases

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

main.cpp

copy
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

main.cpp

copy
123456789101112
#include <iostream> void increment(int* num) { (*num)++; } int main() { int num = 5; int* p_num = &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.

Tarefa

Swipe to start coding

Write a function that swaps the values of two integer variables using pointers.

  1. Create a function swap that takes two pointers to integers as parameters.
  2. Inside the function, use a temporary variable to store the value of the first variable.
  3. Assign the value of the second variable to the first variable using pointer dereferencing.
  4. Assign the value from the temporary variable to the second variable using pointer dereferencing.
  5. In main, call swap, passing the addresses of the two variables.

Solução

solution.cpp

solution.cpp

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 4
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

close

Awesome!

Completion rate improved to 5.88

bookPointers Use Cases

Deslize para mostrar o 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

main.cpp

copy
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

main.cpp

copy
123456789101112
#include <iostream> void increment(int* num) { (*num)++; } int main() { int num = 5; int* p_num = &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.

Tarefa

Swipe to start coding

Write a function that swaps the values of two integer variables using pointers.

  1. Create a function swap that takes two pointers to integers as parameters.
  2. Inside the function, use a temporary variable to store the value of the first variable.
  3. Assign the value of the second variable to the first variable using pointer dereferencing.
  4. Assign the value from the temporary variable to the second variable using pointer dereferencing.
  5. In main, call swap, passing the addresses of the two variables.

Solução

solution.cpp

solution.cpp

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 4
single

single

some-alt