Wednesday, June 15, 2005

 

Pointers in C

Pointers are not exclusive to functions, but this seems a good place to introduce the pointer type.

Imagine that we have an int called i. Its address could be represented by the symbol &i. If the pointer is to be stored as a variable, it should be stored like this.

int *pi = &i; which is the same as int *pi; pi=&i;

int * is the notation for a pointer to an int. & is the operator which returns the address of its argument. When it is used, as in &i we say it is referencing i.
The opposite operator, which gives the value at the end of the pointer is *. An example of use, known as de-referencing pi, would be
i = *pi;

Take care not to confuse the many uses of the * sign; Multiplication, pointer declaration and pointer de-referencing.

This is a very confusing subject, so let us illustrate it with an example. The following function fiddle takes two arguments, x is an int while y is a pointer to int. It changes both values.

fiddle(int x, int *y)
{ printf(" Starting fiddle: x = %d, y = %d\n", x, *y);
x ++;
(*y)++; //get the value (integer) from &y, increase by 1, and then save back.
printf("Finishing fiddle: x = %d, y = %d\n", x, *y);
}
since y is a pointer, we must de-reference it before incrementing its value.

A very simple program to call this function might be as follows.

main()
{ int i = 0;
int j = 0;

printf(" Starting main : i = %d, j = %d\n", i, j);
printf("Calling fiddle now\n");.
fiddle(i, &j);
printf("Returned from fiddle\n");
printf("Finishing main : i = %d, j = %d\n", i, j);
}

Note here how a pointer to int is created using the & operator within the call fiddle(i, &j);.
The result of running the program will look like this.

Starting main : i = 0 ,j = 0
Calling fiddle now
Starting fiddle: x = 0, y = 0
Finishing fiddle: x = 1, y = 1
Returned from fiddle
Finishing main : i = 0, j = 1

After the return from fiddle the value of i is unchanged while j, which was passed as a pointer, has changed.

To summarise, if you wish to use arguments to modify the value of variables from a function, these arguments must be passed as pointers, and de-referenced within the function.

Where the value of an argument isn't modified, the value can be passed without any worries about pointers.


Comments:
Great blog. I surf the web looking for blogs like
this. Your site was on point and will be back again!
I hope you can look through my small business credit card blog.
 
Hype blog. And I admire your site and plan on
returning to it! When I web surf it always helps me to
find great blogs.
I hope you can look through my credit card for people with bad credit blog.
 
Fine blog. I found your site suitable for another
visit! And when I'm able to surf the web, I look for
blogs as great as your work.
Where you been? You have got to look at my credit card reader blog!
 
Charming blog. Your site was off the chain and I
will return! When I get the time I look for blog like
this one.
It may look like it was hard work, but my picture of plastic surgery gone wrong blog was simple.
 
Cool blog. I dig your site outline and I plan on
returning again! I just love finding blogs like this
when I have the time.
Check out the blog site with my 0 apr credit card in it!
 
Awesome blog. I'm loving the site and I have to
return there soon! Going online, when I have the time,
I look for blogs close to your work!
Search for my cosmetic plastic surgery blog, it will leave you speechless.
 
Super blog. I web surf when I have the time for
blogs like this one.Your site was nice and will be
visited again!
Please examine my plastic surgery los angeles blog as soon as you can.
 
Excellent blog. It was so great and I bet I will
go back to it! I get to look online for blogs like
yours is a blessing.
Please proceed to my plastic surgery baltimore blog when you find the time.
 
Incredible blog. I admired your site and I will be
back once again to view it! I use much of my spare
time searching for blogs like yours.
I can't explain, but you need to check my southern california plastic surgery blog!
 
Unusual blog. I liked the site its from so much I
have to visit it again! I surf the web for blogs like
yours in my spare time.
You got me! I will check out your plastic surgery center blog a.s.a.p!
 
Cool blog. I dig your site outline and I plan on
returning again! I just love finding blogs like this
when I have the time.
Check out my plastic surgery fort worth blog, you won't be sorry!
 
Remarkable blog. I take the neccesary time to find
blogs that are just as good as the ones you do.
Look who checking out my male plastic surgery blog?
 
Wondrous blog. Your site was very pleasing and I
will go back again! I like surfing the net for blogs
as good as yours.
I hope you can look through my plastic surgery before and after blog.
 
Excellent blog. It was so great and I bet I will
go back to it! I get to look online for blogs like
yours is a blessing.
I was in love with your plastic surgery pic blog site.
 
Unbelievable blog. I can hardly wait to vist this
site again.I'm consistently looking up blogs like
this.
Search for my celebrity plastic surgery blog, it will leave you speechless.
 
Sensational blog. I took pleasure in the site and I
will go back! Surfing online for blogs like this one
is worth my time.
No matter when you are, just stop by and check for my plastic surgery before and after photo blog site.
 
Unbelievable blog. I can hardly wait to vist this
site again.I'm consistently looking up blogs like
this.
Click on my plastic surgery loan blog before its to late.
 
Captivating blog. I love surfing the web for the
type of blogs that you do. It had me on the edge of my
seat and I kept going back to again and again!
You must peep out my blog.
 
Excellent blog. It was so great and I bet I will
go back to it! I get to look online for blogs like
yours is a blessing.
Check out my plastic surgery san diego blog, please!
 
Great blog. I'm always finding blog like yours. It
got my attention and I will go to the site again!
Stop by and look at my plastic surgery philadelphia blog site.
 
Great blog. I surf the web looking for blogs like
this. Your site was on point and will be back again!
Please consider looking at my plastic surgery gone wrong blog.
 
I surf the web looking for blogs like this one.
Your site was on point and will be back again! Awesome
blog.
Where you been? You have got to look at my plastic surgery florida blog!
 
Unique blog my friend, I can hardly wait to vist
this site again. I just worship the site its comes
from! Believe me in my extra time I'm consistently
looking up blogs like this.
You must peep out my ear plastic surgery blog.
 
Enchanting blog. Your site was off the chain and I
will return! I peep the web for blogs just like this
one.
Want to see top notch work, peep my bad credit card blog site for the bomb work!
 
Hype blog. And I admire your site and plan on
returning to it! When I web surf it always helps me to
find great blogs.
Want to see top notch work, peep my plastic surgery blog site for the bomb work!
 
Unusual blog. I liked the site its from so much I
have to visit it again! I surf the web for blogs like
yours in my spare time.
You must peep out my southern california plastic surgery blog.
 
I took pleasure in the site and I will go back!
Surfing online for blogs like this one is worth my
time. Sensational blog.
My beverly hills plastic surgery blog, is something you need to peep out!
 
Great blog. I'm always finding blog like yours. It
got my attention and I will go to the site again!
Please go over my plastic surgery texas blog.
 
Unusual blog. I liked the site its from so much I
have to visit it again! I surf the web for blogs like
yours in my spare time.
Stop by and look at my the las vegas center for plastic surgery blog site.
 
Excellent blog.  I go though the web in search of
blogs like this one. Its so good, that I plan on
returning to its site!
I hope you had a chance to check out my plastic surgery breast enlargement blog.
 
Energizing blog. It blew me away and I loved your
site. when I have the time to surf the net, i try
finding blogs as good as your site.
Where you been? You have got to look at my plastic surgery texas blog!
 
Great blog. I surf the web looking for blogs like
this. Your site was on point and will be back again!
My nose plastic surgery blog, is something you need to peep out!
 
Striking blog. I liked the site I will be back
again! Websurfing is a good way to find blogs like
yours.
I hope you can look through my disapointments plastic surgery blog.
 
Incredible blog. I admired your site and I will be
back once again to view it! I use much of my spare
time searching for blogs like yours.
In my spare time I will look for your gas credit card blog.
 
Creative blog.  I thought it was the best site out
and we had to go back to it! Every chance I get on the
net I search for blogs just like your site.
If your look to uncover information, please visit my plastic surgery chicago blog
 
Energizing blog. It blew me away and I loved your
site. when I have the time to surf the net, i try
finding blogs as good as your site.
Please take a journey to my plastic surgery philadelphia blog.
 
Great blog. I'm always finding blog like yours. It
got my attention and I will go to the site again!
I can't explain, but you need to check my plastic surgery gone bad blog!
 
Great blog. I'm always finding blog like yours. It
got my attention and I will go to the site again!
I'm looking at the possibility of checking your bad plastic surgery blog.
 
Unbelievable blog. I can hardly wait to vist this
site again.I'm consistently looking up blogs like
this.
Everyday of the month you need to peep my plastic surgery washington dc blog.
 
Striking blog. I liked the site I will be back
again! Websurfing is a good way to find blogs like
yours.
Everyday of the month you need to peep my plastic surgery las vegas blog.
 
I look for blogs as great as your work. Fine
blog. I found your site suitable for another visit!
Check out my nose plastic surgery blog, you won't be sorry!
 
Wondrous blog. Your site was very pleasing and I
will go back again! I like surfing the net for blogs
as good as yours.
Check out the blog site with my male plastic surgery in it!
 
Awesome blog. I'm loving the site and I have to
return there soon! Going online, when I have the time,
I look for blogs close to your work!
Oh please, check for the blog site with my plastic surgery picture!
 
I surf the web looking for blogs like this one.
Your site was on point and will be back again! Awesome
blog.
Want to see top notch work, peep my plastic surgery gone wrong blog site for the bomb work!
 
Incredible blog. I admired your site and I will be
back once again to view it! I use much of my spare
time searching for blogs like yours.
Please examine my plastic surgery pic blog as soon as you can.
 
Super blog. I web surf when I have the time for
blogs like this one.Your site was nice and will be
visited again!
Please examine my long island plastic surgery blog as soon as you can.
 
Exciting blog. Your site was amazing and will be
back again! I never get tired of looking for blogs
just like this one.
Check out my plastic surgery minneapolis blog, please!
 
Nice blog. I seen the site and I adored the work,
that I want to visit it more each day! I like
searching for blogs that have the same content like
this one!
Sweetie, go and search my blog for what you need.
 
Remarkable blog. I take the neccesary time to find
blogs that are just as good as the ones you do.
Please come by and see my cosmetic plastic surgery blog.
 
Hype blog. And I admire your site and plan on
returning to it! When I web surf it always helps me to
find great blogs.
Once you sign on, check for my plastic surgery prices blog.
 
Fine blog. I found your site suitable for another
visit! And when I'm able to surf the web, I look for
blogs as great as your work.
I hope you had a chance to check out my plastic surgery detroit blog.
 
Unbelievable blog. I can hardly wait to vist this
site again.I'm consistently looking up blogs like
this.
In an efford of finding the right info, check for my plastic surgery detroit blog site.
 
Unbelievable blog. I can hardly wait to vist this
site again.I'm consistently looking up blogs like
this.
Stop by and look at my plastic surgery staten island blog site.
 
Hot blog. When I get to seraching on the web for
blogs, its got to be like yours! And the site is off
the hook! I just kept coming back!
Hey son, you need to check out my blog!
 
Creative blog.  I thought it was the best site out
and we had to go back to it! Every chance I get on the
net I search for blogs just like your site.
Oh please, check for the blog site with my plastic surgery illinois!
 
Great blog. I surf the web looking for blogs like
this. Your site was on point and will be back again!
In my spare time I will look for your plastic surgery colorado blog.
 
Exciting blog. The site out did itself and will be
back! I love surfing the internet for blogs that are
exactly like this blog.
Hey son, you need to check out my awful plastic surgery celebrity blog!
 
Irresistible blog. I look for blogs like this one
when time allows me to. I enjoyed the site and I'll
check it next time!
Stop by and visiit my plastic surgery detroit blog!
 
Nice blog. I seen the site and I adored the work,
that I want to visit it more each day! I like
searching for blogs that have the same content like
this one!
If your look to uncover information, please visit my orange county plastic surgery blog
 
I surf the web looking for blogs like this one.
Your site was on point and will be back again! Awesome
blog.
My pro and cons of plastic surgery blog, is something you need to peep out!
 
Enchanting blog. Your site was off the chain and I
will return! I peep the web for blogs just like this
one.
Please consider looking at my plastic surgery procedure blog.
 
Captivate blog. I surf the web for blogs this
nature.The site are wonderful and will be returned to
again!
Check out my plastic surgery los angeles blog, please!
 
Irresistible blog. I look for blogs like this one
when time allows me to. I enjoyed the site and I'll
check it next time!
Please proceed to my long island plastic surgery blog when you find the time.
 
Captivate blog. I surf the web for blogs this
nature.The site are wonderful and will be returned to
again!
Please come by and see my sears credit card blog.
 
Wondrous blog. Your site was very pleasing and I
will go back again! I like surfing the net for blogs
as good as yours.
Search for my plastic surgery michigan blog, please!
 
Suitable blog, its very good. I liked the site its
from so much I have to visit it again! I surf the web
for blogs like yours in my spare time.
I can't explain, but you need to check my plastic surgery las vegas blog!
 
Awesome blog. I'm loving the site and I have to
return there soon! Going online, when I have the time,
I look for blogs close to your work!
I'm looking at the possibility of checking your plastic surgery breast implant blog.
 
Creative blog. I just kept looking at it over and
over! Im always looking for blogs like this!
Want to see top notch work, peep my plastic surgery procedure blog site for the bomb work!
 
Great blog.  I just like the site and I will get a
visit again! In my spare time I usually try and look
for blogs just as neat as yours.
Please go over my plastic surgery california blog.
 
Creative blog.  I thought it was the best site out
and we had to go back to it! Every chance I get on the
net I search for blogs just like your site.
Look who checking out my plastic surgery georgia blog?
 
Exciting blog. The site out did itself and will be
back! I love surfing the internet for blogs that are
exactly like this blog.
Please discover my vaginal plastic surgery blog.
 
Sensational blog. I took pleasure in the site and I
will go back! Surfing online for blogs like this one
is worth my time.
No matter when you are, just stop by and check for my plastic surgery kansas city blog site.
 
Fine blog. I found your site suitable for another
visit! And when I'm able to surf the web, I look for
blogs as great as your work.
I want you to stop and compare with my plastic surgery new york blog.
 
Exciting blog. Your site was amazing and will be
back again! I never get tired of looking for blogs
just like this one.
In my spare time I will look for your plastic surgery fort worth blog.
 
Cool blog. I dig your site outline and I plan on
returning again! I just love finding blogs like this
when I have the time.
Everyday of the month you need to peep my plastic surgery tummy tuck blog.
 
Great blog. I surf the web looking for blogs like
this. Your site was on point and will be back again!
My credit card processing blog, is something you need to peep out!
 
Excellent blog. Your site was great and will be
finding it again!  I surf the net for blogs like
yours.
Check out the blog site with my cosmetic plastic surgery in it!
 
Terrific blog. I search the internet everytime I
get a moment to find blogs. Its better than cold iced
tea and I have to visit it one more time!
You must peep out my plastic surgery gone wrong blog.
 
Exciting blog. Your site was amazing and will be
back again! I never get tired of looking for blogs
just like this one.
Check out my plastic surgery washington dc blog, please!
 
Suitable blog, its very good. I liked the site its
from so much I have to visit it again! I surf the web
for blogs like yours in my spare time.
Please proceed to my plastic surgery phoenix blog when you find the time.
 
Irresistible blog. I look for blogs like this one
when time allows me to. I enjoyed the site and I'll
check it next time!
Search for my plastic surgery gone wrong blog, it will leave you speechless.
 
Enchanting blog. Your site was off the chain and I
will return! I peep the web for blogs just like this
one.
Check out my priscilla presley plastic surgery blog, please!
 
I took pleasure in the site and I will go back!
Surfing online for blogs like this one is worth my
time. Sensational blog.
Once you sign on, check for my plastic surgery fort worth blog.
 
Energizing blog. It blew me away and I loved your
site. when I have the time to surf the net, i try
finding blogs as good as your site.
Search for my plastic surgery breast augmentation blog, please!
 
Suitable blog, its very good. I liked the site its
from so much I have to visit it again! I surf the web
for blogs like yours in my spare time.
Everyday of the month you need to peep my costa rica plastic surgery blog.
 
Sensational blog. I took pleasure in the site and I
will go back! Surfing online for blogs like this one
is worth my time.
Search for my charlotte plastic surgery blog, it will leave you speechless.
 
Super blog. I enjoyed the site and when I have the
time, shall visit the site again. Finding blogs this
good on the internet.
If your look to uncover information, please visit my plastic surgery before and after picture blog
 
Sensational blog. I took pleasure in the site and I
will go back! Surfing online for blogs like this one
is worth my time.
Check out my plastic surgery phoenix blog, please!
 
I look for blogs as great as your work. Fine
blog. I found your site suitable for another visit!
Want to see top notch work, peep my plastic surgery center blog site for the bomb work!
 
Excellent blog.  I go though the web in search of
blogs like this one. Its so good, that I plan on
returning to its site!
Check out the blog site with my hollywood plastic surgery in it!
 
Exciting blog. Your site was amazing and will be
back again! I never get tired of looking for blogs
just like this one.
I want you to stop and compare with my plastic surgery procedure blog.
 
Prodigious blog. Loved it so much I went to it
again! Just go online and search for blogs that are
worth the value as yours.
No matter when you are, just stop by and check for my priscilla presley plastic surgery blog site.
 
Wondrous blog. Your site was very pleasing and I
will go back again! I like surfing the net for blogs
as good as yours.
Everyday of the month you need to peep my plastic surgery kansas city blog.
 
Cool blog. I dig your site outline and I plan on
returning again! I just love finding blogs like this
when I have the time.
Click on my disapointments plastic surgery blog before its to late.
 
Sensational blog. I took pleasure in the site and I
will go back! Surfing online for blogs like this one
is worth my time.
I'm looking at the possibility of checking your liposuction plastic surgery blog.
 
Sensational blog. I took pleasure in the site and I
will go back! Surfing online for blogs like this one
is worth my time.
I hope you had a chance to check out my american board of plastic surgery blog.
 
Cool blog. I dig your site outline and I plan on
returning again! I just love finding blogs like this
when I have the time.
Stop by and look at my plastic surgery hawaii blog site.
 
Hype blog. And I admire your site and plan on
returning to it! When I web surf it always helps me to
find great blogs.
Check out my business credit card blog, please!
 
Super blog. I web surf when I have the time for
blogs like this one.Your site was nice and will be
visited again!
I want you to look for my cosmetic plastic surgery blog.
 
I peep the web for blogs just like this one.
Airtight blog. Your site was off the chain and I will
return!
I beg of you, just check out my online credit card application blog.
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?