分类: C/C++
2006-06-23 13:09:31
///////////////////////////////////////////////////////////////////////////
// 10. Pointers - A Better Understanding //
// By: Moe Elzubeir //
// AKA kuru @EFnet's #c //
// Email: moe@bigger.com //
///////////////////////////////////////////////////////////////////////////
o Intro
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.
o What is a pointer ?
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 convinient way of doing things is another matter that
is best discovered as you go along discovering the "ways of pointers" ;)
o Declaring 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 ;)
o Using Pointers
Now let's do a little exchange. We will exchange the contents of 2
varilables using pointers.
int myvar1 = 13, myvar2 = 69, temp;
int *mypntr;
mypntr = &myvar1; /* In case you don't know the &myvar1 means the address of
which the contents of myvar1 are stored and not the
actual value of myvar1 & is called the address
operator] */
temp = *mypntr; /* copies the value of the variable that mypntr is
pointing to into temp, in which case myvar1 */
*mypntr = myvar2; /* copies the contents of myvar2 into the address pointed
to by myprntr */
myvar2 = temp; /* copies temp into myvar2 - simple */
We have just exchanged the values using pointers. This is pretty useless it
seems, but it's a good exercise to get a grip on the concepts.
o The Address Operator
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 woth 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.
o Pointers to Arrays
Pointers and arrays are closely related and thus confused together. Let's
consider this segment of code :
float salary[50];
foat *fload_ptr;
Now lets consider this :
float_ptr = salary;
float_ptr = &salary[0];
Well, these 2 statements are the same thing. They do the exact same thing,
which is have float_ptr point to the first element of the salary array.
A string constant (eg, "C Scene") is stored as an array of chars with a null
terminator added as the last char. But, look at this :
char *char_ptr = "C Scene";
Not only does it define the pointer but also initializes it to the address
of the first character in the string. The same statement could have been
written like :
char *char_ptr;
char_ptr = "C Scene";
o Pointers to Pointers
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 argument (argc, argv) :
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