1.
Introduction 2.
What is a pointer? 3.
Declaring Pointers 4.
Using Pointers 5.
The Address Operator 6.
Pointers to Arrays 7.
Pointers to Pointers 8.
Conclusion
by Moe Elzubeir
last updated 2000/01/31
(version 1.1)
also available as XML
IRC: kuru @EFnet's #c
Email: moe@bigger.com
| |
Many beginners have a hard time grasping the concept of pointers. And even
the some-what intermediate programmers face some problems with pointers.
Since it seems to be the biggest obstacle for all, I thought maybe a little
explanation of how it all works would suit the needs.
|
| |
In the simplest of words, a pointer is a variable that holds the memory
address of another variable. Think of it as the mailing address you put on
an envelope. That address points to the house, or po box of the person you
want to send the letter to. The pointer simply holds the address. The same
way the envelope holds the address of the person. It points to the person.
Get it? Pretty simple concept.
Now why we find this a convenient way of doing things is another matter that
is best discovered as you go along discovering the "ways of pointers". ;)
|
| |
Declaring a pointer is very simple: char *mypntr; .
In this case the pointer points to the memory address of a char variable.
Note that it should point to a char variable and not a float or any other
variable. The compiler might start groaning and producing strange sounds if
you don't do it right. ;)
|
| |
I think it's time for a little light turned on to the address operator.
There are a few things you should know about it.
There are a few things you cannot do with the address operator. Some may
seem obvious, but are worth pointing out.
1. var_address = &13;
You cannot use the address operator with constants.
2. var_address = &(value + 69);
You cannot use the address operator with expressions involving
operators such as + and / given the definition that value=9 (for example).
3. var_address = ®
Cannot use the address operator by preceding register variables
given the definition register reg .
|
| |
Although it might strike you funny, but this will become a daily thing for
you if you start doing any coding. Yes, it is a pointer that points to a
pointer that points the address of a certain variable.
A good example would be the command line arguments vector argv
in the prototype of int main(int argc, char **argv); .
The char **argv , which is also sometimes written
char *argv[] , is a pointer to a pointer.
|
| |
There is a lot more to do with pointers. But I am truely tired and
exhausted. And will finish off some other issue. I have only covered the
"basics" of it all.
Moe Elzubeir
|
This article is Copyright © 1997 by C-Scene. All Rights Reserved. |