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
Excellent blog. Very nicely explained each and every thing. If possible can you please add some notes on strings. Thank you
Thanks a lot. Yes, definitely i am working on it. You will see the new post on Strings within few days.
I m just a beginner in C, bt still i find ur work gr8…
Thanx for creating such a site.
This is nice site for beginners. Thanks for creating this wonderful site… 🙂
you can use %p to print address of pointers in linux/gcc instead of %u 🙂
why pointer are used in c ?
-Tamajit Basu
Good site
thankyou verymuch i have got a clear cut idea on c langauge after studying this.
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
Thanks a lot.
very well written and organized tutorials
Excellent blog..very good for beginners.. just add some more programmes to make it more clear.
Very good blog……Please add more concepts….
nice blog
beautiful site for all those who wanna learn C
excellent site for beginners
Thanks a lot… 🙂
Thank you for the great site:)
what will be the value of c and *(*b) in the last example?
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…
thanks
waoo this is grt site.
i dont understand c ,,,,,,,,,???
what to do
This site makes C more understandable for me……
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)
Good work
We have recently started a new forum.
http://forum.learnconline.com/
Readers can now post their queries and get it resolved.
Thanks,
LearnCOnline Team
this site is spoon feeding me love you author…thanks a lot lot
Could you explain some practical examples with pointer for pointer? as **p please… I ll be grateful with you a lot
printf(
why address is always 65524 in all machines.
An excellent blog for beginners, thank you
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
i don’t understand c?please someone help me?
do you have a blog on c++ like this ????
Hello Yuvaraj,
We do have C++ tutorial website.
Please visit: http://www.learncpponline.com
Thanks,
LearnCOnline Team
Extremely pleased by ur description thanks a lot… Valuable one