»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 01-21-2004, 08:38 AM   #1 (permalink)
Registered User
 
Iturea's Avatar
 
Join Date: Jan 2004
Posts: 80
Iturea is on a distinguished road
Kids C++ - Pointers and Practicality

I am new to C++, well actually programming in general. Why would you use a pointer? What is the practicality of it? I have programmed in Visual Basic and VBScript for websites, but I have never come across the need to use a pointer. Now I am taking a C++ class in school and I need to use pointers for different assignments but I still don’t see the practicality of them or why there is a need. I mean isn’t it redundant to point to a location in memory when there is already a variable assigned to the information in that memory location?

Maybe I am looking at this all wrong. Can someone please give me an educated definition of what a pointer is, and the “point” of ever using one?

Iturea is offline   Reply With Quote
Old 01-22-2004, 12:59 AM   #2 (permalink)
Registered User
 
Join Date: Jan 2004
Location: Michigan
Posts: 6
pkanaby is on a distinguished road
Hey there. I'm pretty good with pointers and I think I can pointer you in the right direction a bit . You're right about the pointer simply being a reference to a memory location. But there are many uses for pointers and some of them can be really simple. OK, first I'll give a good example of why you would need to use a pointer.

Say you make a struct containing a few variables (like some ints and a few chars or something). Now you wish to create a variable of your struct data type in your main function (not global). If you write functions to modify the data in the struct, you have to send the struct to the function, right? This is where a problem occurs. C++ does not send the memory location to your function. Instead, it sends all the data from your struct into a new struct for the function to use. If you modify the struct in the function, it modifies a different piece of memory and the changes won't be noticable by your main function. This is where pointers come in. Instead of letting your function accept a struct as input, you can have your function accept a pointer to the struct as input instead. Using this reference, you can treat the memory address as a struct and modify it so the main function sees the alterations. That is a pretty useful aspect of pointers.

OK, I'm not sure how far you want to go into this, but I'll explain another 2 important uses in summary.

Arrays: Arrays are already pointers in C++. When you send an array to a function you always accept it as if it were a pointer. If you didn't do that, then your function would have to copy all that data from one part of memory to another (slow and wasteful). Arrays can also be created by the programmer in a better way.
A standard array in C++ has a set size when you create it. If you go over the limit, then you get an out of bounds error. Instead of using this, you may choose to create a linked list. You would start by making a link data type. This will contain, for instance, the piece of data, and a pointer to the next link. A linked list is simply a starting reference to a point in memory to the first link of this chain. Each link then references the location in memory of the next link. I think you can already see the flexability in that. Think about how much easier and faster it would be to remove a link rather than removing data from an array and shifting all the data after it down a position. This example is just a singly linked list. You can make doubly linked lists by having the links reference(point) back to previous link, you can make trees by adding pointers to more than one next link, or anything else. This is a major foundation piece to data structures.

One last example. This one is pretty abstract because I just recently taught myself since no one else knows how to do it. Mapped functions (at least that's what I call them). When working with tons of functions that are used by calling a location on a map (think of an RPG game where your character causes things to happen when certain tiles are walked on), it's easiest to use function pointers. Instead of filling an array of function pointers, you can even fill the array with references to a smaller array containing the actual function pointers. The code for running a proper function based off the location of a character in an RPG would be a huge jumbled mess of logic. My way looks more like this:

typedef void (*actionfunction)(); // action function pointer type

// Array of indexes
const unsigned char ActionLayer[20*5]={
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

// Array of function pointers
const actionfunction ActionSet[3] = {
DO_NOTHING,Some_Function,Some_Other_Function
};

During the program's run time, a simple call to ActionSet[ActionLayer[x+y*20]]() would run the appropriate function associated with the x,y location on the map. If you understand this you are now a genius because no one else understands how I figured that one out or how it works except me so far. You can probably tell I spend most of my programming time in game developement. It's fun and a great field to be in. Good luck with your programming. I hope you have learned a bit about the importance of pointers and maybe how they work

-Phil (pkanaby@comcast.net)
pkanaby is offline   Reply With Quote
Old 01-22-2004, 10:12 AM   #3 (permalink)
Registered User
 
Iturea's Avatar
 
Join Date: Jan 2004
Posts: 80
Iturea is on a distinguished road
Food Thanks!

Those were excellent examples. That thoroughly helped me understand the use of pointers as well as the power of C++. I did not realize you could create a tiered architecture of arrays to manage paths to functions. I can see the use of this in gaming as your example comprehensively shows as well as applying it to solutions to some of my web application needs when working with so many end user variances. Man what a flow chart that would be to create!!!! Yikes! I could easily get lost, but I need to get used to the framework a little to adjust to the complexity.
Iturea is offline   Reply With Quote
Old 01-22-2004, 08:39 PM   #4 (permalink)
Registered User
 
Join Date: Jan 2004
Location: Michigan
Posts: 6
pkanaby is on a distinguished road
No problem
pkanaby is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 07:43 AM.