![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2004
Posts: 38
Rep Power: 0
![]() |
This tutorial covers the very basics of pointers, really basic stuff - if it is insulting or overly pedantic or confusing please let me know.
People coming to C from other programming languages are often confused by pointers. I'm not going to claim it is the most intuitive thing in the world, but if you actually just play with some code and work through some examples it begins to make sense - really. ![]() C has very few 'primitive' types. char c; int i; float f; c = 'A'; i = 42; f = 3.14159; c is type char, i is type int, f is type float - pretty straightforward. A pointer is kind of like an additional primitive type that is specified with '*'. char *pc; int *pi; float *pf; pc is type pointer to char, pi a type pointer to int, and pf a type pointer to float. The '*' is part of the type and not part of the name. Many people would write these as 'char* pc' rather than 'char *pc' - whichever you prefer is the correct way to do it. So the type of the variables are 'pointer to ...', now what do we assign to those variables? pc = &c; pi = &i; pf = &f; The '&' is the address of operator. That means that pi (pointer to int) holds the address of the variable i. You may have seen that '&' operator before in a tutorial. The following is a bit of tutorial code I remember when I learned C. #include <stdio.h>
int
main(int argc, char **argv)
{
int n;
printf("enter a digit to continue: ");
scanf("%d", &n);
printf("you entered %d\n", n);
return 0;
}You see in the argument to scanf we passed in the address of n rather than the variable itself. The reason for this will not make sense right now, I just wanted to let you know that you may have been using pointers already without even knowing it. ![]() So how else can a pointer be used? The '*' is part of the type of a pointer variable but '*' also serves as the indirection operator. If you remember back to our earlier examples 'int *' is the type of the variable pi, if you print the value of pi itself you will have the address of whatever it points to, but if you print the value of '*pi' then it will peek at what pi points to and that is what gets printed. #include <stdio.h>
int
main(int argc, char **argv)
{
int i = 100;
int n = 200;
int *pi = &i;
/* %x is a printf format arg to print values in hexadecimal */
printf("i = %d, n = %d, &i = %x, &n = %x, pi = %x, *pi = %d\n",
i, n, &i, &n, pi, *pi);
return 0;
}On my system that code prints the following: i = 100, n = 200, &i = bffffcc0, &n = bffffcc4, pi = bffffcc0, *pi = 100 So what does that mean exactly? The 'i = 100 and n =200' should be pretty obvious, the &i and &n are a bit more mysterious - what the program prints is the memory address at which the variable i and n are actually located (I use %x to print the memory addresses because it is traditional to print memory addresses in hex ). If we look at what prints for pi we see that it is the same as &i, the actual value in the variable pi is the address of i. The last part '*pi' applies the indirection operator to pi and displays the value of what pi points at.With me so far? Lets modify that last program a bit to see what happens. #include <stdio.h>
int
main(int argc, char **argv)
{
int i = 100;
int n = 200;
int *pi = &i;
/* %x is a printf format arg to print values in hexadecimal */
printf("initial state)\n");
printf("i = %d, n = %d, &i = %x, &n = %x, pi = %x, *pi = %d\n",
i, n, &i, &n, pi, *pi);
pi = &n;
printf("(pi = &n)\n);
printf("i = %d, n = %d, &i = %x, &n = %x, pi = %x, *pi = %d\n",
i, n, &i, &n, pi, *pi);
*pi = 42;
printf("(*pi = 42)\n");
printf("i = %d, n = %d, &i = %x, &n = %x, pi = %x, *pi = %d\n",
i, n, &i, &n, pi, *pi);
printf("(pi = &i)\n");
pi = &i;
printf("i = %d, n = %d, &i = %x, &n = %x, pi = %x, *pi = %d\n",
i, n, &i, &n, pi, *pi);
return 0;
}This is sort of a "poke it with a stick and see what happens" approach. We start with the same program from the last example, then make a change to some of the values, print out what change we made and then print out the results of the change.On my system it prints the following: (intitial state) i = 100, n = 200, &i = bffffcc0, &n = bffffcc4, pi = bffffcc0, *pi = 100 (pi = &n) i = 100, n = 200, &i = bffffcc0, &n = bffffcc4, pi = bffffcc4, *pi = 200 (*pi = 42) i = 100, n = 42, &i = bffffcc0, &n = bffffcc4, pi = bffffcc4, *pi = 42 (pi = &i) i = 100, n = 42, &i = bffffcc0, &n = bffffcc4, pi = bffffcc0, *pi = 100 One thing is to notice that the address of n and i (&n, &i) never changed. The value of pi was always the address of whichever int variable we had set pi to point to. The only thing thing that might come as a surprise is after the (*pi = 42) line, it immediately changed the value of the variable n that pi was pointed at at that moment, so not only can we look at the thing pointed to, we can change it as well. So the most important thing to get here is that a pointer contains an address of another variable. The two different meanings of '*' as part of the pointer variable type itself and as the indirection operator may be a bit confusing for a while, but as you work with them it will be clear what the '*' means at any given moment. The thing that may remain a bit confusing is what pointers are really good for. Why do you care about the address of a variable when you have the variable itself? I'll put together a tutorial on arrays and memory allocation that I hope will make that a bit more clear. |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Jun 2004
Location: South Africa, Johannesburg
Posts: 301
Rep Power: 5
![]() |
Thanks, I think this will help me, as c++ for dummies, maybe I can understand now.
![]()
__________________
[SIGPIC][/SIGPIC] |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Here's a tip: ditch that book. It's crap. I've never come across a good C++ book (but then, I haven't looked very hard - I learnt C and went from there). Perhaps someone else could recommend something?
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 5
![]() |
A great book for console programming that teaches all the basics:
Deitel and Deitel: How to program C++ ISBN: 0-13-111881-1 Publisher: Prentice Hall They also have C# Jave et al languages... very nicely layed out and good examples and explanations.
__________________
Ravilj's OpenGL Terrain aka WinTerrain Last Updated: 17/01/2005! |
|
|
|
|
|
#5 | |
|
Professional Programmer
Join Date: Jun 2004
Location: South Africa, Johannesburg
Posts: 301
Rep Power: 5
![]() |
Quote:
__________________
[SIGPIC][/SIGPIC] |
|
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 556
Rep Power: 5
![]() |
I learned a lot of C from C by example by Greg Perry...You should try C++ by example
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 |
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 556
Rep Power: 5
![]() |
Nice tutorial...Let me get this straight
If I had int i; and int *pi, and *pi=&i, pi would be the value of i because the address of i was assigned to it?
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 |
|
|
|
|
|
#8 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
pi would be the memory address of i.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 556
Rep Power: 5
![]() |
And *pi would be the value of i?
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 |
|
|
|
|
|
#10 | |
|
Programmer
Join Date: Sep 2004
Posts: 38
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|