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

Started by
6 comments, last by Captain P 15 years, 10 months ago
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
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.
-------------Please rate this post if it was useful.
I suggest learning from a real resource, like Accelerated C++

"you need the & when dealing with structures" isn't teaching you anything
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)
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.
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
Hey,

I understand now. Thanks Everyone!

P.S I'm gonna buy accelerated C++. :)
I'm a member of the "learn it when you need it" camp.
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.
Create-ivity - a game development blog Mouseover for more information.

This topic is closed to new replies.

Advertisement