After a long time, here we are adding a new step in our coding knowledge!
In today’s episode of Learn2Code we are going to discuss pointers, what they are and why they are used. Pointers are one of the most difficult thing to understand while discussing programming, and the logic behind their usage may not always be clear.
So, brace yourself. Pointers are coming!
Memory addresses
When a variable is declared, its value is saved in a location on the machine’s memory, which is divided in small 1 byte cells. In order for a program to retrieve the variable’s value, each memory cell has a unique address; to retrieve this address, the &
operator must be used:
// Declaring variable
int var = 5;
// Printing variable address
std::cout << “Address: “ << &var << std::endl;
When printing the address, something like this should appear:
Address: 0x6dfed4
As it can be noted, this is not the value stored inside the var
variable; instead, what is printed is the memory address in which the variable’s value is stored.
What is a pointer?
Now that the concept of memory address has been explained, we can move on to the first question: what is a pointer?
Simply put, a pointer is a variable that stores as value an address in memory. So, basically, a pointer points to a memory cell in which something is stored.
To declare a pointer, the *
operator is used and can be initialized with a variable’s address. Keep in mind that the pointer must be of the same type as the variable’s address pointed by it. For example:
// Declare pointer
int* ptr = &var;
// Printing pointer
std::cout << ptr << std::endl;
This way, the pointer now points to the memory cell in which var
is stored. What do you think will be printed?
The correct answer is the following:
0x6dfed4
So, can a variable access the value of the variable whose memory space is pointed by the pointer? Of course, the answer is YES! To do so, the deference operator *
is used:
std::cout << “The variable’s value is: “ << *ptr << std::endl;
By using this syntax, the following is printed:
The variable’s value is: 5
A pointer can also be declared using a custom made type, such as a class or a struct. Let’s see an example:
// Person class
class Person {
Person();
~Person();
int age;
string name;
void walk();
void run();
}
Once that the Person
class is defined, a pointer of type Person
can be declared:
// Declare pointer
Person* tuma;
Very simple! Now that the pointer is declared, it is also possible to access attributes and methods belonging to the class through the arrow operator ->
:
// Set age
tuma->age = 35;
// Print age
std::cout << “Person’s age: “ << tuma->age << std::endl;
// Call method run()
tuma->run();
Why using a pointer?
Pointers are very useful in different occasions:
Allow the programmer to access and modify the same instance of an object from different parts of the code
Allow to avoid copying big objects when passing them to functions (pass-by-reference)
Allow to access specific chunks of memory when needed (this is the concept of arrays, which we will talk about in a later lesson).
After this lesson, it should be easier for you to understand the Bitcoin code. In fact, we encountered many pointers throughout our deep dive.
See you soon guys!
Tuma
You are clearly pointed in the right direction.