My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Your First Step With Pointers

Your First Step With Pointers

My Purple Serie

Fatima Senouci's photo
Fatima Senouci
·Feb 11, 2022·

3 min read

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.

po.jpg

Firstly, to understand pointers, we should distinguish between statistique and dynamique variables at the beginning. These two types are different in:

  1. How they use memory and its presentation

  2. 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:

image.png

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 :

image.png

NameAdressValue
x#1005
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 :

image.png

  • 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.

image.png

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

image.png

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