Introduction
Pointers are a part of advanced algorithms and are more important than a basis to understand the data structure, so I wrote this article to clarify the idea of the process of this algorithm So I wish you learned and had a good time reading.
Firstly, to understand pointers, we should distinguish between statistique and dynamique variables at the beginning. These two types are different in:
How they use memory and its presentation
Several bytes, allocation, and executing time.
to clarify the idea I give here a definition of a dynamic variable
Dynamique variable :
It is referred to a variable that can be allocated at any point in the program and that can be freed at the end of execution.
Variable representation :
Well we know that each variable can be represented by :
- Name, address, value
Exp:
So after this introduction, we can now define Pointers
Pointers's definition :
A pointer is one of the data types, such as "integer, double.".It's called a pointer because each variable it holds is a memory address. In addition, this pointer address points to a memory area and this area contains data as "object, structure, and integer......."
An example to understand more :
Name | Adress | Value |
x | #100 | 5 |
p | #1 | #100 |
So as a result, we can get from the table that the P-value is 5, so x is the value of P, and P is the address of x.
P and *P
So now we are ready to give the difference between P and *P
P: is a static variable and is data where its type is an address.
*P : is a dynamic variable, is the content of P, and is the area allocated by malloc "we will talk about malloc "
So the pointer's data type is a type of the variable pointed by this pointer. Thus, we gain that pointer points to the dynamic variable noted "*P."
The deployment steps of the pointer
- Declaration :
- Memory Space Allocation
We use this deployment by malloc function. As we know, every function has parameters and a return value. So malloc reserves bytes and the return is the address of those bytes.
sizeof function allows retrieving the number of bytes used to represent a data type.
- Use of both variables P and *P
The name of the pointer is P, and its value is " *P ".
*P = 5;
- Releasing memory space
using free(),function
For the static variables, once the execution is finished the memory space is freed but on the other hand for the pointers once you have allocated memory space and after having finished the execution the value of the pointer returns to null but the memory space is still reserved, that's why we should use the free() function.
Conclusion
To conclude, after reading this article, you can now get a general understanding of pointers, but this is insufficient. You should know how to use them and practice some exercises. For that, I planned another article containing this concept.
Check my count to get news and follow me on Twitter