Share on Google+Share on Google+

Pointer in C

In the previous tutorial you come to know what is data structure, dynamic memory allocation. Now in this tutorial you will see the how we declare a pointer variable. How it works with a very simple example.

About Pointer

A pointer is a variable which contain the memory address. It also points to a specific data types. Three operator are commonly  used  when dealing with pointer.

1    &    address operator
2    *  de-referencing operator
3    ->   structure pointer operator


In this example you will see how pointer works.

In the above figure you see that c is a variable of char data-type and it has value "A" inside. The address of variable c is 0X3000

Now in this figure you see that c is a variable of char data-type and it has value 'A' inside. The address variable c is 0X3000. And a pointer variable cptr of char data-type. The cptr pointer variable having the address of c variable rather value. So this is one the important difference between a general variable and pointer variable is that a pointer variable contains memory address.

A complete example

#include <stdio.h>
void main()
char c ='A';
char *cptr;
printf("\nThe address of c is\t%p",&c);
printf("\nValue of c is\t\t%c",c);
printf("\n\nThe address of cptr is\t %p",&cptr);
printf("\nValue of cptr is\t%p",cptr);
printf("\nAccess variable which cptr point to is\t%c",*cptr);


From the output of above example you can see that the address of these two variables are different and the pointer variable holding the address of another variable which it point to.

Download this code



Posted on: April 29, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null