
In this chapter we are going to study:
- Pointers.
- References.
- How to use them.
Pointers
When we have to create a new variable, we type:The variable "x" is saved to memory, then memory keep that value in a row.
If we change the value of the variable "x" like:
int x = x + 100;Then, the memory row that keeps the last value x = 10, now is changed to x = 110.
How to use
In the following example we can see a few lines of code to study how to use pointers and their operators:For example, the variable (int *my_pointer) is a variable of pointer type. That kind of variable can keep a memory address that keeps an integer data type.
If we need to know a memory address about another variable or element, we have to use the &(ampersand) operator. In the code we can see how we keep the memory address of variable (int age) to a integer pointer variable (int *my_pointer).
my_pointer = &age;
References
References are exclusive of C++ language.Example
int main(void) { int code; int &code2 = code; code2 = 5; }In this case, we created a variable called (code) and a reference variable called (code2) that calls (code). Now all operations that we would do with (code2) is performed with the variable (code)
This is a fast and summarized tutorial about pointers and references for C/C++.
Alex - aferrerdeveloper.com
No hay comentarios:
Publicar un comentario