Logo

dev-resources.site

for different kinds of informations.

Pointers

Published at
4/16/2022
Categories
c
programming
pointers
Author
arghyasahoo
Categories
3 categories in total
c
open
programming
open
pointers
open
Author
11 person written this
arghyasahoo
open
Pointers

One of the fundamental concepts in computer programming is a variable. If you are studying computer science, or programming in general, chances are you know what a variable is. In simple terms,

A variable is a container which holds a value

A variable can hold values of certain datatypes, such as int, char, float, bool etc. Pointers are nothing but a special kind of variable. Instead of storing a value, it stores address of a variable.

In case all of this jargon didn't make any sense, we will dig deeper into this.

A computer primarily consists of two types of storage, Primary Memory and Secondary Memory, whenever we execute a program, all of the variables declared in it gets stored in the Primary Memory a.k.a the Main Memory a.k.a the RAM. Now the variables can be anywhere in the RAM, but how could we locate them?

Lets say, you want to invite one of buddies to your house, but he has never visited your house before, then how would he find it? He will need an address. With an address he can locate your house exactly. But without one, he has to roam around the entire world until he finds your house. That doesn't sound like a fun task...does it?

Similarly, every variable that is stored in the primary memory has a specific address assigned to it. Different variables can not reside in the same address. Just like, the address of your house and your friends house can not be same unless you both live in the same house.

Now you need to send your address to your friend. How would you do that? Let's say, your send a text message to your friend containing your address. your friend receives it, and comes to your place by locating the address and making his way to it.

Let's retrace his steps, first he receives the text message, which reveals your address, then he locates the specific address to find your house. This is an example of a pointer. Just like the text message contained the address of your house, a pointer contains the address of a variable. It does not contain the actual value of the variable, rather an address where to find it. Imagine if your text message contained your entire house in it. As the text message contained a reference to your actual house, a pointer contains a reference to an actual variable. Dereferencing the pointer reveals the value of the variable it is referencing.

Meme

Let's talk CODE ๐Ÿ‘จโ€๐Ÿ’ป

  • In C Language, a pointer is declared as *<pointer_var_name> like int *p;
  • The datatype of a pointer is same as the datatype of the variable it is storing. If a pointer referencing a variable which is storing integer value, the pointer will be an integer pointer, if the variable is storing a character, the pointer will be called a character pointer.

For example

int a = 5;
int *ip = &a;  // '&' is called the "address of" operator, and the '*' is known as "dereference" operator Here, the address of a is fetched and stored inside ip

char c = 'c';
char *cp = &c;
Enter fullscreen mode Exit fullscreen mode

Let's dig deeper into the code. For the first example, let the assume that a is stored in memory location 100. a has a value of 5 and ip contains the address of a. So, technically the value of ip will be 100. Dereferencing ip will result in the value of a which is 5.

Single Pointer

Pointers can also hold address of another pointer. They are called double pointers

int a = 5;
int *ip = &a;  // ip holds the address of a
int **pp = &p;  // pp holds the address of p, which holds the address of a
Enter fullscreen mode Exit fullscreen mode

In the above example, dereferencing ip once gives the value of a. As it directly points to a, but in case of pp to get the value of a you need to dereference twice. As, pp is holding the address of ip and ip is holding the address of a.

Double Pointer

This way, pointers can be chained together.

Pointers are SLOW! ๐ŸŒ

Whenever you execute a program, all the variables declared in it is stored in that programs stack frame which resides in RAM. Now, during the calculations, CPU fetches variables from the RAM into its internal registers. This memory access is not the fastest thing in the world. This process takes up quite a few nanoseconds. So, in case of directly accessing a variable, CPU needs to fetch the variable only once from the memory and it gets its value. But in case of pointers, multiple trips are required to the RAM from CPU to get a value. In case of single pointers, first the address of the variable is fetched and then again, the value variable is fetched from that specific memory address. In case of double pointers, it makes three trips to the RAM before it gets the value of the variable. So, longer the chain, the more time it will take to fetch the variable.

Let's take the previous scenario. Instead of providing direct address to your house, you text your friend the address of your local grocery store, there a man hands him the address to your nearest rail station, there someone gives him the address to your house, This will surely take longer time, instead if he was given direct address to your house.

That's all folks, hope this article gives you "yet another" viewpoint towards pointers. Pointers are often misunderstood by a lot of newbie programmers. When broken down to its basics, they are really simple and easy to grasp.

pointers Article's
30 articles in total
Favicon
CS50 - Week 4
Favicon
Pointers in Modern C
Favicon
Go: Pointers & Memory Management
Favicon
Pointers and Arrays
Favicon
Understanding Memory Management, Pointers, and Function Pointers in C
Favicon
Pointers : what are they pointing to?
Favicon
Pointers in Go Programming Language
Favicon
An ode to Stacks and Pointers in Go!
Favicon
The Absolute Minimum Every Software Developer Must Know About Pointers
Favicon
Pointers in C programming.
Favicon
How constant is const in C?
Favicon
Pointers in C Programming: How Hard?๐Ÿค”
Favicon
In C++, is a free function taking a struct as an argument faster than a class with a member function to do the same thing?
Favicon
How to assign an Address, contained inside a string, to a pointer in C
Favicon
Understanding pointers in Go
Favicon
Reversing a Linked List: A Hallway Analogy
Favicon
Exploring the Power of Pointers in C Programming
Favicon
Learn It Once: โ€œGolang Pointers are Powerfulโ€
Favicon
C++23: std::out_ptr and std::inout_ptr
Favicon
Pointers , Arrays & Strings in C
Favicon
C - Even more pointers, arrays and strings
Favicon
C - Pointers, arrays and strings
Favicon
A common pitfall when using sizeof() with pointers
Favicon
Pointers
Favicon
Storing references of pointers in containers in C++
Favicon
C++ basics: Pointers vs iterators
Favicon
I broke production 3 times in 3 weeks - Part II
Favicon
WTF*&n is Pointers in Golang (Bahasa Version)
Favicon
I broke production 3 times in 3 weeks - Part I
Favicon
Golang 101: ฤฐลŸaretรงiler (Pointers)

Featured ones: