DEV Community

Arjav Patel
Arjav Patel

Posted on

Understanding pointers in c through an Analogy

Scenario 1:

Let's assume you have a locker A. The locker is open. You can view the contents of the locker and change it.

Scenario 2:

Let's assume you have a locker A and B. Locker A is closed and locker B is open. The key to locker A is there in locker B. You can use the key from locker B and see and change the contents of locker A.

Let's try to make an analogy in the playgrounds of c++.

int main()
{
    int a = 10; // Declaring the variable
    printf("%d", a) // viewing the content of the variable
    a = 20; // changing the contents of the variable
}

Connect to scenario 1. Assume 'a' is locker and is open. You can view its content and change it.

Now let's connect to scenario 2. But before that, let us get familiar with the syntax of the pointers.

int main()
{
    int *b; // Means 'b' is a pointer that is of type int. Here we are declaring the pointer
    float *c; // Means 'c' is a pointer that is of type float.
}

Now let's dive a little deep and connect to scenario 2.

int main()
{
    int *b; //1 declaring pointer but not assigning it anything
    int a = 10; //2 declaring the int variable and assigning it a value;
    printf("%d\n", a); //3 Scenario 1
    b = &a; //4 assigning the pointer 'b' the location of 'a'.
    printf("%d\n", *b); //5 viewing the contents of 'a' through 'b'.
    *b = 20; //6 changing the contents of 'a' through 'b'.
}

Heavy, huh?

Let's break it down piece by piece.
Line 1: We are declaring pointer as mentioned in the previous code snippet. Piece of cake. No worries here.
Line 2: We are declaring and initializing the int variable. Good until here!
Line 3: Printing the int variable 'a' and connecting to scenario 1. Locker A is open and we can view the contents of the locker. We can also change the contents of the locker. 'a = 30', this will change the contents of 'a'.
Line 4: Here where the magic happens. What this statement means is you are giving the key to Locker A to Locker B. So now using the key from locker B, you can change and view the contents of Locker A. In lines 5 and 6 is what we do.
Line 5: View the contents of Locker A through key which is there in Locker B.
Line 6: Change the contents of Locker A through the key from B.

That's what pointer is. Yes, believe me, that's it. Everything else about pointer rotates around this concept.

In the next post, we will discuss why we use a pointer in the first place? Why make complex things when everything can be done through simple chunks? How pointers are used in array? How pointers are used in Linked List?

Hugs and Bugs are most welcome. If you like the post, just hit one of those emoticons so that I know I am able to help you. If not, please comment so that I can improve or clear your doubts!

More links for your reference:
https://www.geeksforgeeks.org/pointers-c-examples/
https://beginnersbook.com/2017/08/cpp-pointers/
https://gist.github.com/ericandrewlewis/720c374c29bbafadedc9

Top comments (0)