Your Next Step With Pointers

Your Next Step With Pointers

ALGORITHMS -DATA STRUCTURE

Introduction

In this article, I will further explain the concept of pointers (data structure) with some examples to practice what we have mentioned in the last article(part 1). To understand this concept you should check first the article Parte one and welcome back to train with me.

Example number 1:

We assume that we have a class of 50 students and each person is defined by:

  1. His code,
  2. His family name [ max = 20 character];
  3. His name [ max = 20 character];

Propose a data structure using only static variables to manage the students of a class

d1.PNG

Propose a data structure using only dynamic variables to manage the students of a class

d2.PNG

Calculate the memory space using the two methodes for a class of 20 students

  1. Method One : (2+20+20)*50 =2100 bytes

Since we use here static variables, So if we need to calculate the memory area for one student is always equal to the storage space for 50 students, so the result will be always 2100 bytes

While the second method, the mechanism is different

S= 50+(2+20+20)*20 = 8900 bytes

Since we use here pointers we consume just the requested memory size, 50: Is the size of the pointer array, since they announced that each pointer occupies one byte

Vignette de Vlog YouTube Scolaire Geeky Violet Blanc et Bleu Clair (1).png

For Method number 1 (Using Static Variables) :

Waste and mismanagement of data since static variables occupy memory until the end of execution.

For Method number 2 :

Better optimization of memory space since dynamic variables can be created and deleted at any time

Memory space allocation using the for as loop

d3.PNG

assignment of variables

d4.PNG

Pointer operations: real machine

addition / subtraction of pointers

d5.PNG

Comparison between pointers:

the C language allows us to compare pointers using

== , != ,< ,>,<=,>= , null

Point a simple variable that already exists (statistic variable )

d7.PNG

Point -an-array

We suppose T is an array of integers and P is a pointer to an integer

d8.PNG

Exemple

d10.PNG

Conclusion

These are the most popular instructions that you will need to use pointers and to build a solid basis in this domain I hope that you found my article useful, I did my best to clarify the idea and present it in a simple way