Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Mupp

Pointers?

This topic is 5373 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, i got a question about structure pointers, if i do something like this
struct movie
{
   char title[50];
   int year;
};
 
and in main i do this
movie amovie;
movie *pmovie;
pmovie = &amovie;
 
whats this good for? whenever i change pmovie,
pmovie->year = 1560;  
i could have easily changed
amoiv.year = 1560; 
its the same thing right? if thats the case, then why use the pointer shit there? [edited by - Mupp on February 6, 2004 8:07:55 AM]

Share this post


Link to post
Share on other sites
Advertisement
that is a great question..why use it there ?

that is absolutely useless......seriously...useless

"A soldier is a part of the 1% of the population that keeps the other 99% free" - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA

Share this post


Link to post
Share on other sites
Need clarification... are you asking what good a pointer is in your specific example, or what good pointers are in general?

Share this post


Link to post
Share on other sites
That sounds as a boring "introduction to pointers" example. Such examples are (for some reason) not often made to make sense, but to show the syntax of pointers. There are no point in using pointers like that, but there are uses, I assure you.

Share this post


Link to post
Share on other sites
Well.. i dont know really, could you tell me a few advantages with pointers? maybe point me to a better tutorial, or maybe just show me a short code example of a good use of pointers?

Share this post


Link to post
Share on other sites
One of the big uses of pointers in C++ is when you allocate memory on the run.

If you would like an array of integers, but don''t know how many when you compile the program, then you can use:
int* myArray = new int[x];

Or when creating objects. If you have a game engine, where you store all your objects in objectList, then you could do something like:
objectList.Add(new MyClass());

and so on..

Share this post


Link to post
Share on other sites
Further, when you have complex objects, passing a pointer to a function instead of the actual object will save your program from having to make a copy of that object first.

I use pointers all the time. (Maybe more than I should).

Share this post


Link to post
Share on other sites
If we''re talking about C, then pointers are the only way.

But in C++ applications, pointers shouldn''t be used very often. For example, using a Standard Library container such as vector or list is strongly preferred to dynamically allocating an array. Furthermore, references are safer than pointers because references are guaranteed to point to a valid object, yet they offer the same performance benefits. So, only where implementing the internals of certain classes or interfacing with code written in other languages should you use pointers in C++.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!