Pointers in C

Pointers in C is one of the excellent feature introduced in C. It makes the programming very interesting.

A pointer in C is a variable that represents the location (rather than the value) of a data item.

Talking like a layman, C pointers points to an object or something. Let us understand C pointers in detail.

Before learning the basics of pointers, let us understand how the variable is stored in the memory.

When we declare a variable and assign a value to the variable, we are actually naming the memory location at which the value will be stored. The value will be stored at the memory location and not in the variable.

We can display this address using ampersand(&) as follows:

int a=10;
printf(“%u”,&a) ; //displays the address of the variable a
printf(“%d”,*(&a)); //displays the content at that address

In the above code snippet,
Value 10 is assigned to the variable a. Internally, a is the name give to the memory address.
Using ampersand we can print the address of a as shown above.
Similarly, we can display the content at that address using *. The ‘*(&a)’ is read as “content at address of a”.

This address can be stored in a variable as follows.

b = &a;

As we all know that we should declare a variable before using it, we cannot directly use it to assign an address.
As the variable b is pointing to the memory location, there is different way to declare this variable. This declaration can be done as follows:

int *b;
b = &a;

Here, b is pointing to the memory address. Hence, b is called a pointer.
After assigning the address to the pointer variable b, we can get the value at the address at which it is pointing using *.

Let us understand the C pointers in detail using an example.
Example:

void main()
{
    int *b, a=10;
    b=&a;
    printf(“\nThe address of a : %u”, b);
    printf(“\nThe address of a : %u”,&a);
    printf("\nThe address of a : %u",*(&b));
    printf(“\nThe value of a : %d”,a);
    printf(“\nThe value of a : %d”,*b);
    printf(“\nThe value of a : %d”,*(&a));
}

Output:

The address of a : 65524
The address of a : 65524
The address of a : 65524
The value of a : 10
The value of a : 10
The value of a : 10

Let us understand the concept of pointers to pointer. Pointers to pointer is a pointer that points to another pointer. Here pointer will store the address of another pointer.

Let us understand this using an example.
Example:

void main()
{
    int *b, a=10, **c;
    clrscr();
    b=&a;
    c=&b;
    printf("\nThe address of a : %u", b);
    printf("\nThe address of a : %u",&a);
    printf("\nThe address of a : %u",*(&b));
    printf("\nThe address of a : %u",*c);
    printf("\nThe value of a : %d",a);
    printf("\nThe value of a : %d",*b);
    printf("\nThe value of a : %d",*(&a));
    printf("\nThe value of a : %d",*(*c));
    getch();
}

Output:

The address of a : 65524
The address of a : 65524
The address of a : 65524
The address of a : 65524
The value of a : 10
The value of a : 10
The value of a : 10
The value of a : 10

Also See:
Demonstration on Pointer Operations
Operations on Pointers

You may also like...

38 Responses

  1. Anonymous says:

    Excellent blog. Very nicely explained each and every thing. If possible can you please add some notes on strings. Thank you

  2. LearnCOnline says:

    Thanks a lot. Yes, definitely i am working on it. You will see the new post on Strings within few days.

  3. Neha Mahaur says:

    I m just a beginner in C, bt still i find ur work gr8…
    Thanx for creating such a site.

  4. Rahul Saxena says:

    This is nice site for beginners. Thanks for creating this wonderful site… 🙂

  5. Anonymous says:

    you can use %p to print address of pointers in linux/gcc instead of %u 🙂

  6. tamajit says:

    why pointer are used in c ?
    -Tamajit Basu

  7. Anonymous says:

    Good site

  8. sushma says:

    thankyou verymuch i have got a clear cut idea on c langauge after studying this.

  9. Hello Blogger!
    Well I am not really a beginner to “C” or any other language. I was just reviewing some websites to refer to my juniors to learn C online, and my friend, I’ve seen many other “C” learning websites, but I admit, that this blog is the best of all -” FOR BEGINNERS” .. yeah the blog doo lack of many concepts and deep explanations, but it in a way provides the best overview of “C” and the best way to start up learning computer languages.

    People who read from this blog will really get the basic concepts of “C” in one go.
    all the very best mate, and you’ve really done a great job in describing all these concepts in the simplest manner.
    All the very best for your future.

    Regards,
    Sarang Dravid
    ADMIN
    http://www.itechnobuzz.com

  10. very well written and organized tutorials

  11. anuj says:

    Excellent blog..very good for beginners.. just add some more programmes to make it more clear.

  12. Prince says:

    Very good blog……Please add more concepts….

  13. Anonymous says:

    nice blog

  14. Anonymous says:

    beautiful site for all those who wanna learn C

  15. abhishek says:

    excellent site for beginners

  16. LearnCOnline says:

    Thanks a lot… 🙂

  17. Anonymous says:

    Thank you for the great site:)

  18. Anonymous says:

    what will be the value of c and *(*b) in the last example?

  19. Anonymous says:

    Pointers are very deep deep concept, this artcle is a good one but need to und pointers in detail… 100’s of questions on pointers in interviews…

    expect some more depth in this article…

  20. Anonymous says:

    thanks

  21. Anonymous says:

    waoo this is grt site.

  22. i dont understand c ,,,,,,,,,???
    what to do

  23. Mandeep kaur says:

    This site makes C more understandable for me……

  24. Anonymous says:

    cant we use like this to find the address?
    printf(“\nThe address of a : %u”,&(*b));
    I want to know whether &(*b) is equal to *(&b)

  25. Anonymous says:

    Good work

  26. LearnCOnline says:

    We have recently started a new forum.
    http://forum.learnconline.com/

    Readers can now post their queries and get it resolved.

    Thanks,
    LearnCOnline Team

  27. Anonymous says:

    this site is spoon feeding me love you author…thanks a lot lot

  28. Anonymous says:

    Could you explain some practical examples with pointer for pointer? as **p please… I ll be grateful with you a lot

  29. Anonymous says:

    printf(

  30. Anonymous says:

    why address is always 65524 in all machines.

  31. Anonymous says:

    An excellent blog for beginners, thank you

  32. LearnCOnline says:

    Thank you.
    We have recently launched a new website to learn C++ Online. The website is currently in progress and many more articles on C++ is in queue.

    Visit the below link to learn C++ online:
    http://www.learncpponline.com

    Thanks,
    LearnCOnline Team

  33. Anonymous says:

    i don’t understand c?please someone help me?

  34. do you have a blog on c++ like this ????

  35. LearnCOnline says:

    Hello Yuvaraj,

    We do have C++ tutorial website.

    Please visit: http://www.learncpponline.com

    Thanks,
    LearnCOnline Team

  36. vignesh says:

    Extremely pleased by ur description thanks a lot… Valuable one

  1. March 1, 2014

    […] Pointers in C […]

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet