Jump to content
  • Advertisement
Sign in to follow this  
Raxis

Structures: A part of them I can't figure out.

This topic is 3830 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

I am totally befuddled, I have been learning from a tutorial (cprogramming.com) and I am on structures. I can't figure something out.

#include <iostream>

using namespace std;

struct xampl {
  int x;
};

int main()
{  
  xampl structure;
  xampl *ptr;
  
  structure.x = 12;
  ptr = &structure; // Yes, you need the & when dealing with structures
                    //  and using pointers to them
  cout<< ptr->x;    // The -> acts somewhat like the * when used with pointers
                    //  It says, get whatever is at that memory address
                    //  Not "get what that memory address is"
  cin.get();                    
}



xampl structure; I don't understand the useage of this. Couldn't we just go int x = 12;? -thanks

Share this post


Link to post
Share on other sites
Advertisement
Of course you could. The example only serves to show you the syntax of structs (and classes), not the proper use of them. And the use of & has nothing to do with structs here; it has to do with pointers, but that's not what you asked about.

Consider, for example, what happens when you try to make a game - say, an RTS. You'll need to keep track of possibly hundreds of units, each with its own position, speed, heading, model, firing state, health, ammo, current order and so on. Keeping track of all those loose variables for all those objects is pretty much impossible without making a huge mess; every time a new unit is created, new instances of each variable must be made, for example.

That's why you have structs and classes. You can place all those variables in a struct, and when a new unit is created you just instantiate that struct. Keeping track of things suddenly becomes simple. It's even more powerful when you add member functions, because then each object becomes its own state machine.

Share this post


Link to post
Share on other sites
I suggest learning from a real resource, like Accelerated C++

"you need the & when dealing with structures" isn't teaching you anything

Share this post


Link to post
Share on other sites
I second Accelerated C++. Its very good, very fast paced. It may be daunting to a beginner however if they never programmed but it is a very useful book even if you've done some programming.

In my experience I usually find it a good idea to work through some smaller books then get a big fat book and work through it. This gives you the acheivement satisfaction and some knowledge when you finish the smaller books and then the big book solidifies your learning. (This approach works for me, your milage may vary)

Share this post


Link to post
Share on other sites
I think you missed the key point of structures - they can have more than one variable in them. So you can have struct.x, struct.y, struct.z and, what do you know, you just created a convinient point/vector structure!

Also, structures (and classes) can have methods (functions), private and protected data and inherit from one another, adding to their usability.

And, as been pointed out, & has NOTHING to do with structures. It returns the address of your variable, so that it can be assigned to a pointer.

I agree with what others said - pickup a good book and read through it.

Share this post


Link to post
Share on other sites
Quote:
Original post by CDProp
I'm a member of the "learn it when you need it" camp.

The problem with that is that you may not know when there's a better alternative. Just keep learning and use what's best for a given job.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!