C Tutorial – How to use Pointers

To make full use of the C Programming language, you have to have a very good understanding of pointers. For most people it will take some time to fully understand pointers. So be patient. You have to learn pointers because they are used everywhere in the C language. Once you master the use of pointers, you will use them everywhere. OK, enough pep talk, let’s start.

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

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

Pointers are also used by experienced programmers to make the code more efficient and thus faster.

So why use pointers? Why don’t we use arrays to create data structures?

The answer is simple. With an array you have to declare its maximum size (for every dimension) at the beginning. Let’s say you create an array that can hold a maximum of twenty megabytes. When the array is declared, the twenty megabytes is claimed. Now this time you have only data for ten megabytes.

(A next time it could be fifteen megabytes or five megabytes). So in this case ten megabytes of memory is wasted, because only ten megabytes from the twenty is used.

This is where pointers come in. With pointers, you can create dynamic data structures. Instead of claiming the memory up-front, the memory is allocated (from the heap) while the program is running. So the exact amount of memory is claimed and there is no waste. Even better, memory not used will be returned to the heap. (Freed memory can be used for other programs).

Pointer basics

To better understand pointers, it sometimes helps to compare a “normal variable” with a pointer.

When a “normal variable” is declared, memory is claimed for that variable. Let’s say you declare an integer variable MYVAR. Four bytes of memory is set aside for that variable. The location in memory is known by the name MYVAR. At the machine level that location has a memory address.

A pointer differs in the way that a pointer is a variable that points to another variable. A pointer holds the memory address of that variable. That variable contains a value. Pointers are also called address variables because they contain the addresses of other variables.

Example: We have a piece of memory with a start address of 0x2000. That piece of memory contains a value and is named MYEXAMPLE.

(This is in fact a variable). We also have a pointer MYPOINT. In this case, our pointer MYPOINT contains the address 0x2000. This is the address of MYEXAMPLE so we say MYPOINT points to MYEXAMPLE. So there is the pointer and the value pointed to. (You can use a pointer in a certain way to get the value at the address to which the pointer points). Many novice programmers get pointers and their contents confused.
So from now on every pointer starts with the extension ptr_ .  (for example: ptr_MYPOINT).

To declare a pointer you have to put an * in front of its name. A pointer can be typed or untyped. (A typed pointer points to a particular variable type such as an integer. An untyped pointer points to any data type). See the following example of a declaration of a typed pointer and an untyped pointer:


	int main()
		int *ptr_A;		/* A typed pointer */
		void *ptr_B;	/* A untyped pointer */
		return 0;

Put the address of an integer into a “pointer to an integer” by using the & operator (address operator)
in front of the integer, to get the integer’s address.

Let’s take a look at an example:


	int main()
		int x;
		int *ptr_p;

		x = 5;
		ptr_p = &x;

		return 0;

Note: The integer x contains the value 5 on a specific address. The address of the integer x is copied in the pointer ptr_p. So ptr_p points to the address of x. In short: ptr_p = &x; means, “Assign to ptr_p the address of x.”

To access the value of the integer that is being pointed to, you have to dereference the pointer. The * is used to dereference a pointer. Take a look at the following example:


	int main()
		int x,y;
		int *ptr_p;

		x = 5;
		ptr_p = &x;
		y = *ptr_p;
		printf("%d\n", y);

		return 0;

Note: The integer x has a value of five. The pointer ptr_p gets the address of integer x.
The value pointed to is *ptr_p. (in this case five). So the integer y now contains the value of five.

Take a look at the following example:


	int main()
		int x;
		int *ptr_p;

		x = 5;
		ptr_p = &x;

		*ptr_p = 10;		

		printf("%d\n", x);

		return 0;

Note: First the ptr_p contains the address of the integer x (ptr_p = &x). Then we change the value of the integer where ptr_p is pointing to (*ptr_p = 10;). The integer x now equals ten.

That’s all for this tutorial. You should now understand the fundamentals of pointers. If not, read it again until you’ll get it, before reading the next C tutorial on pointers.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 60 responses to “C Tutorial – How to use Pointers”

Why not let us know what you think by adding your own comment!

  1. swamy on October 8th, 2010:

    ur explaination is very well.i did not understand pointers up to read this material . when i read this material i fully understand basic concepts of pointers. very thankful to u.

  2. payges on October 13th, 2010:

    great stuff…many thanks for clarifying

    PS ‘In short: p = &x;’

    should be

    ‘In short: ptr_p = &x;’

    I was trying to figure out why it is so important to have to declare a pointer, why not just use a normal variable to store teh address, but i can see now if you do not do that you cannot dereference and get to the value of the original variable being pointed to, which is ultimately very important to be able to do…

    Is that correct?

  3. florin on January 4th, 2011:

    Thank u very much :) It is so clear.

  4. feenix on April 6th, 2011:

    thanx for detail explanation..it really helps me.thnx a lot…:)

  5. Suresh on April 17th, 2011:

    Sir thanks.u r explaining very easy manner.

  6. RAHUL on April 23rd, 2011:


  7. Prosenjit on June 6th, 2011:

    Thank you.Your discussion on the basic about pointers is really very good.The basics of pointers are now clear to me.

  8. sagar on June 29th, 2011:

    thanks ur tutorial is very helpful.

  9. Sunny on July 16th, 2011:

    It’s awesome man;
    I now know a lot about pointers;
    but am still having a problem{
    Can U help me write a program to swap values of 2 variables
    i.e. take 2 integers a,b;
    give them values,call your swap function and print the result;
    the result must have swapped values;

  10. admin on July 17th, 2011:

    @sunny – Take a look at C Tutorial – more on pointers, it has just the thing you are looking for. Good Luck.

  11. Sunny on July 17th, 2011:

    Ya thnx I found it

  12. venkatrao on July 21st, 2011:

    thanking you sir

  13. shipra on August 24th, 2011:

    Sir, thank you so much.

  14. shailaja.bangaru on September 6th, 2011:

    thanku sir,it is very easy to understand.

  15. nithya on September 14th, 2011:

    Such a awesome website to learn C language. I understand basic pointer, thanks a lot sir

  16. syed junaid on October 3rd, 2011:

    wow ur website is so usefull. i liked ur method of explaning,the concept is clear and easy.

  17. ndindiriyimana on November 16th, 2011:

    you present very well

  18. shashi on November 30th, 2011:

    its quit well explained tutorial on pointers,

  19. lavanya on December 3rd, 2011:

    really fantastic

  20. murli on December 8th, 2011:

    you make me understand malloc

  21. widi on February 6th, 2012:

    thank’s….this article is very helpful for beginners…

  22. Rushabh on July 8th, 2012:

    wow.. thanxx a lot sir..
    I had too much difficulty in understanding pointers.. u made it so so so simple..:)

  23. SYED MEHER ALI SHAH on August 9th, 2012:

    thanks a lot .sir your have done a good job :) takecares

  24. dhanda vijay on September 4th, 2012:

    Thanks A lot sir i have get more knowledge in pointer … you had done such a great job

    please post some difficult topics from c language i am very eager to learn c language

    thanks and regards

  25. UMESH on September 20th, 2012:

    very clear explanation……. thank u..

  26. Rahul on September 24th, 2012:

    thank very much sir,i full understand basic conepts of pointer

  27. Eve-Marie on September 26th, 2012:

    thanks a finally got it! very well explained! :) thanks again!

  28. Nasrul on October 23rd, 2012:

    thanks sir, ur explanation about pointer is very easy to understand

  29. Shaan Royal on October 24th, 2012:

    gud work man :)

  30. CSC209Cramming « SLog of the most illogical mind on October 25th, 2012:

    […] http://www.codingunit.com/c-tutorial-how-to-use-pointers […]

  31. Avanish Verma on November 19th, 2012:

    thanks a lot admin……….:)

    with regards
    Avanish Verma

  32. ramesh on December 5th, 2012:

    excellent explanation

  33. sajani wimalachandra on December 25th, 2012:

    thanks for this explanation. it is good explanation

  34. Tayyab Riaz, on December 25th, 2012:

    its really a very Good.Thanx

  35. poojatiwari on January 1st, 2013:

    thank u…………its really nice explanation…..

  36. Ranga on January 9th, 2013:

    This is a good tuts for who are in confusion about C

  37. Santa on February 5th, 2013:

    Great tutorial, cleared up the fogginess around pointers and how to use them. Thanks!!

  38. Orkut Mahmud on February 12th, 2013:

    awesome tutorial

  39. Ayaz Hussain Abbasi, Lecturer Computera @ SZABIST Campus Larkana on February 25th, 2013:

    I highly appreciate the efforts put in here for the assistence beginers in the amazing world of the programming.

  40. Swarup on March 7th, 2013:

    Organised and Well-Composed. Good to Understand.

  41. Zach Elwood on April 8th, 2013:

    Nice explanation. I had read a few other sites that didn’t explain it as well and now it makes a lot more sense.

  42. venky on April 25th, 2013:

    Good explanation easy to understand thank you!

  43. Andyman on May 1st, 2013:

    As a physics student learning C for computational modelling at university I have to say that this page beats my lecture material. Its a bit of an awkward/complicated topic that is explained very clearly. thank you!

  44. rohit gupta on May 8th, 2013:

    it was really very simple method to explain pointers

  45. jayanthi on May 16th, 2013:

    your explanation was very nice,i understood about pointers

  46. nush on June 17th, 2013:

    plz help me when we can use from ***a in private part of a class?

  47. Namdev on June 20th, 2013:

    thanks a lot

  48. karuppusamy on July 2nd, 2013:

    i refer some website to understand pointer concept but it’s difficult. but referring this website it becomes very easy so very thank to you sir.

  49. Sivakumar on July 18th, 2013:

    its a very good explanation. i now understood about pointer.. and i have taken notes of it..
    thank u so much..

  50. santhosh on July 26th, 2013:

    its really very useful for me thanks a lot

  51. dd on July 30th, 2013:

    its really usefull

  52. kiruthika on August 12th, 2013:

    Thanks for giving these basic programs, it was very easy to understand.

  53. Mr. C on September 12th, 2013:

    Good use of examples. Made this very clear.

  54. Pragatheeswaran on October 30th, 2013:

    need more progarms

  55. asitha on November 3rd, 2013:

    thank u very much…..
    Its quitely very useful to us…

  56. Muhammad Khalid on December 7th, 2013:

    thanx for detail explanation..it really helps me.thnx a lot…:) Its amazing way to explain…….

  57. sakurai on February 6th, 2014:

    thank you so much… 😀

  58. santosh kumar yadvendu .(RICLA) on April 4th, 2014:

    A million of thanks sir…..

  59. mwaizson on April 8th, 2014:

    so nice thanks for these stuffs

  60. sai on May 16th, 2014:

    Excellent explanation……..