Learn About the pointer in easiest way – coderinme

The students of computer science have a nightmare about the pointer, It’s difficult to learn pointer but it’s more difficult when we want to implement it.
We normally face 3 types of problem

    1. What do u mean by &a and *a or **&a
    2. if *a is used in method overloading then when it is not used, or when it is useful
    3. Find the output of this program if the statements are like
      void fun(void *p);
      int i;
      int main()
          void *vptr;
          vptr = &i;
          return 0;
      void fun(void *p)
          int **q;
          q = (int**)&p;
          printf("%d\n", **q);

so we decided to give you a basic understanding that how a pointer works, what is the output and how it is useful.

Learn About the pointer in easiest way

A pointer is a variable which contains the address in memory of another variable.
or A pointer is an object whose value points to another value stored elsewhere.
A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.

Like we know the name of a building normally. Because of that we know the value/person residing in that building.
But in-spite of that we have stored address of that building (add = &building), and when ever we want to know the person then we just do dereferencing like *add= * (& building)

Ok now you understand that if we know the address we can find the value so inspite of value just play with address that what pointers do.

a pointer points to a memory address

Learn About the pointer in easiest way
& it means Address of variable
* Value at that address

in the above pic the upper left of all cell is address and at the middle of every cell that is value.
cell 7 that is address 7 the value (*7) is 5 so we can say *7=5
and address of value 5 (&5) is 7 i.e. &5= 7 that is known as y variable y=5
&y=7 and *(&y)=5

From the above pic find the value of
* & & & & * X

Next Part Pointer

Now you have a theory about the pointer, it’s notation, address, referencing and dereferencing.
you understand that if we know the address we can find the value so in spite of value just play with an address that what pointers do.
a pointer points to a memory address

Pointer Why and When we use

Let’s take an example
Remember that question in which we have to write a program to interchange the value of two variables. Note:- (both ways using temporary variable and not using any other value)
But we can do it using pointer

void intSwap (int *pa, int *pb){
    int temp = *pa;
    *pa = *pb;
    *pb = temp;

You need to know the following –

int a = 5; // an integer, contains value
int *p; // an integer pointer, contains address
p = &a; // &a means address of a
a = *p; // *p means value stored in that address, here 5

Instead of number if there is string then

void charSwap(char* a, char* b){
    char temp = *a;
    *a = *b;
    *b = temp;

So, when you swap like this. Only the value will be swapped. So, for a char* only their first char will swap.

Now, if you understand char* (string) clearly, then you should know that you only need to exchange the pointer. It’ll be easier to understand if you think it as an array instead of a string.

void stringSwap(char** a, char** b){
    char *temp = *a;
    *a = *b;
    *b = temp;

Pointers are used (in the C language) in three different ways:

To create dynamic data structures. Array, Linked List

To pass and handle variable parameters passed to functions.
To access information stored in arrays. (Especially if you work with links).

Learn About the pointer in easiest wayLearn About the pointer in easiest way


All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.