|
||
|
GP Mailing List
ATXGPSIG List
|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: Beginner question
The wonderful thing about pointers is that they can point to anything.
Now...lets say you have an array. Lets say you want to be able to hold a
person's information (address, phone number, etc.) all in one element. Now
you can't just put a structure object right into an array. So you would
have to use a pointer to that object. So for example:
struct Address
{
char firstName[50], middleName[50], lastName[50];
char address[50];
/*etc...*/
};
Okay that's the structure. Now lets say I wanted to use an array of these,
like an address book for example. I would make an array of pointers to this
type. Like this:
Address *addressBook[50];
So now with this, each element can point to a whole object. So then I could
access the individual elements like so:
cout<<addressBook[i]->firstName<<endl; /*Prints out the first name of the
address stored at index i.*/
That is one major advantage of them. The ability to point to the beginning
of an object (or a large amount of data).
The second use you will encounter later is when you use what are called
linked lists. In this case, you build your own data type. A Linked List is
a dynamic array which can store any type of data. It is similar to the
College Board's AP Classes. Basically you have two classes. One is called
the Node and the other is the complete linked list. The linked list is
composed of nodes. A node is anything you want it to be, like for example a
node could be a single character in a string. Now the point. For the
linked list to be infinitely expandable. Each Node contains a pointer to
the next Node in the linked list. This is the way they are all linked
together. And all you need to do to add another node to the linked list is
to point the last node's next pointer to the new node you created. Don't
worry about the details of this one. It's just for conversation purpose.
Anyways...let me teach you a little about using pointers. Let's start with
the basics:
Let's make a simple pointer:
int number=5;
int *ptr;
Now let's point the pointer to the variable:
ptr=&number;
The pointer is now set to the address of number (indicated by the &
operator).
Now to prove the point:
cout<<*ptr<<endl;
That would print out the value of number. Now if you did this:
cout<<ptr<<endl;
You would see a funky hex number. This is the address at which the variable
resides.
Now yes...this is a pointless example. You will hardly ever have to use
pointers like this...that was just a teaching point.
The next use (and is actually a legimate one) is with strings:
char *string;
Now this little fellow here to your amazement is the same as:
char string[20];
Of course the nice thing about the pointer is that there is no reason to set
a size when you start. It will just point itself to the memory the string
occupies.
To recap:
With a pointer the * operator in front of the variable represents the actual
value at the address. The one without is the actual address.
*ptr is the value of number.
ptr is the address of number.
number is the value of number.
&number is the address of number.
I hope this helps you a little bit.
Next Use...An Array of strings.
This is one of the best examples I can think of that's easy to understand.
char* arrayOfStrings[50];
Now before I explain it, can you see what this does? Okay...the type is?
Good! It's a pointer to a character variable or a string of them. And how
many of those pointers? Good! There are 50 of them. So basically you can
have 50 strings stored in this variable.
This is all I will do for now. The next examples are using data structures
and I want to wait for you to be ready for that first.
Mike Main
> Hey there,
> I am a beginner and I am covering pointers and have a few questions.
The
> book I am using is not giving me the best explanation. The code makes
tons of
> sense but I want to know a little more about what a pointer is.
> As I understand it a pointer variable is pointing to a variable
location in
> memory. So when passed in a function it is not exactly being passed but
where
> the variable is located is being pass. What is a pointer exactly used for?
Is
> the previous explantion the answer. If so when is the best time to
declare
> and use pointers? What is a benefit of a pointer? Is it just speed? Any
help
> you all could give would shed light on this concept.
>
> William
>
>
> =================================================================
> The GameProgrammer.Com mailing list is for the open discussion
> of any topic related to the art, science, and business of
> programming games. This list is especially tolerant of beginners.
> We were all beginners once
>
> To SUBSCRIBE or UNSUBSCRIBE please visit:
> http://gameprogrammer.com/mailinglist.html
>
=================================================================
The GameProgrammer.Com mailing list is for the open discussion
of any topic related to the art, science, and business of
programming games. This list is especially tolerant of beginners.
We were all beginners once
To SUBSCRIBE or UNSUBSCRIBE please visit:
http://gameprogrammer.com/mailinglist.html
|
|