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

Contenido del Curso

C++ Pointers and References

C++ Pointers and References

1. Pointers Fundamentals
2. Pointer Arithmetic
3. References Fundamentals
4. Dynamic Memory Allocation

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.

cpp

main

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.

cpp

main

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.

Tarea
test

Swipe to show code editor

  • Create a function that swaps values of two variables.
  • Call this function.
  • Output the values of variables after the swap.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4
toggle bottom row

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.

cpp

main

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.

cpp

main

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.

Tarea
test

Swipe to show code editor

  • Create a function that swaps values of two variables.
  • Call this function.
  • Output the values of variables after the swap.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4
toggle bottom row

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.

cpp

main

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.

cpp

main

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.

Tarea
test

Swipe to show code editor

  • Create a function that swaps values of two variables.
  • Call this function.
  • Output the values of variables after the swap.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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.

cpp

main

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.

cpp

main

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.

Tarea
test

Swipe to show code editor

  • Create a function that swaps values of two variables.
  • Call this function.
  • Output the values of variables after the swap.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 1. Capítulo 4
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt