Pointers in 5 minutes.

Venkatesh Ellaboina
codeburst
Published in
6 min readFeb 13, 2018

--

Source: Pexels

Dealing with pointers could be a daunting task. It happens to everyone who is trying to learn pointers. I’m gonna take you through small building blocks, learning the essence and working of pointers at each step.

Pointer:
A pointer is a variable which holds the address of other variable of the specified data type(like int,float,char). In programming we basically use pointers to store the other variable’s address.

A pointer variable is declared with a ‘*’ before it.

int *a . a(int type pointer here) can hold only the addresses of int type variables. Assigning a different type of variable is a constraint violation.

Understanding pointers declaration:

The most important point in pointers.

int *a=&b is a easy way of assigning the address of b to a. This doesn’t mean *a is equal to ‘address of b’.
Look at the below explanation to understand easily.

int *a=&b is actually a simple notation of ,
int *a;
a=&b;

Let's look at an example:

int b=10; 
int *a=&b;

Assume b’s address is 1000 and a’s address is 4000.

Note: All the addresses mentioned from here are not actual values but are used for easy demonstration.

It is as simple as that.
value of b is 10
address of b (which is &b) is 1000
value of a (which is address of b) is 1000
value of *a (which is the value of b) is 10

‘*’ is used to dereference a pointer. A pointer on dereferencing goes to the pointing address and looks for the value it is holding.

Equating two pointers:

Let’s take two different pointers of the same data type and see what happens when we assign one pointer to the other.

int b=10;
int c=20;
int *a=&b;
int *d=&c;
a=d;
//in short we are telling 'a' to point to the address 'd'is pointing to

Now a is holding the address of c and not b.

If you reached this point, you are just one step away from the feeling aaahhhh…….Now it makes sense 🧠

The famous Swap values example

Now you know how pointers actually work. But why do we need them? What is the necessity of a pointer?. I’ll take the famous swap values example and illustrate the essence of a pointer.

Suppose you have two variables,
int a=10, b=20;
We are interested in swapping the values of a and b by passing them to a function. let’s write the required code. Please read the code from the main function then look at the swap function.

void swap(int c, int d)
{
int temp;
temp=c;
c=d;
d=temp;
}
void main()
{
int a=10,b=20;
swap(a,b);
printf(“a= %d b= %d”,a,b);
}

The output of the above code is :
a=10 b=20.
But we swapped them right?. This is where we are missing the main point. let’s look at the variables and their values.

Swapping without pointers.

The local variables of the function swap (‘c’ and ‘d’) are changing but not ‘a’ and ‘b’. It is not what we want our function to do.

Now let’s do it using pointers.
Writing the same function but using pointers instead.

void swap(int *c,int *d)
{
int temp;
temp=*c;
*c=*d;
*d=temp;
}
void main()
{
int a=10,b=20;
swap(&a,&b);
printf(“a= %d b= %d”,a,b);
}

Output:
a=20 b=10.
Now, look at it step by step how it is happening.

*c=10 and *d=20.

A detailed explanation is given below. Refer to the above image for more clarity.

I.temp=*c means we are assigning the value of the variable whose address is in c (i.e., ‘a’ which is 10).

temp=10 now.

II.When *d is assigned to *c

  1. First, go to the address d is holding which is 2000 (address of b).
  2. Now get the value at 2000 which is 20 (which is b).
  3. Now go to the address ‘c’ is holding which is 1000 (address of a ).
  4. Now assign the value at b (which is 20) to a.

a=20 now.

III.Similarly *d=temp.

  1. First, get the value at temp which is 10.
  2. Now go the address d holding which is 2000 (address of b).
  3. change the value at b to temp which is 20.

b=10 now.

Thus,
a=20 & b=10. We can clearly see the values swapped.

A dangling pointer:

If the address to which the pointer is pointing to is no longer accessible in the memory then that pointer is called a dangling pointer.

Dangling pointer.

A Null pointer:
A Null pointer is assigned a reserved value which tells us the pointer doesn’t point to anything.

int *ptr=NULL;
On assigning a pointer to null you are just telling it isn’t pointing to anything.

Difference between dangling and null pointer:

To make it more clear let's take an example.

Assume you are a pointer and there are a bunch of apples in front of you each numbered from 1 to n. The apple you are looking at can only be selected by your guru.

Your guru ordered you to look at apple number 10. Once you are looking at specific apple your eyes are fixed on it. You cant even look at other apples for now.

Scenario 1:
Suppose guru interrupted you and placed an obstacle in between you and the apple such that you can no longer see your apple. Now you are trying to look at apple number 10 but you can no longer do it.

Scenario 2:
Suppose you are blind for now(no offense :)). You can’t even see any apple now. That means by default you cannot see anything. But later on, you can look at the apple by guru’s mantra.. :-p.

Now consider both the Scenarios:
In ‘Scenario 1’ you are actually a dangling pointer .Even though you used to look at apple 10 but you no longer can.But on guru’s orders you can look at other apples.

In ‘Scenario 2’ you are actually a null pointer.You are blind. You cant even look at anything. But on guru’s mantra you can get your eyes and on guru’s orders you can look at a specified apple.

Practically, Those apples are the accessible memory by your program,Obstacle is guru deleting that variable, you are the pointer and guru is the person writing code.

By now, You know how pointers work and their necessity in programming. You are all set to play with pointers 🔥 😎

If this post was helpful, please click the 👏 button below to show your support!

Thanks for reading 👍 . Let me know if you have any feedback.

mail: venkateshellaboina@gmail.com
LinkedIn : Venkatesh Ellaboina

--

--