![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Programmer
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0
![]() |
A question about structures
Hey again. I had a question about some code I was trying out.
Quote:
Hell, it all just seems rather redundant to me. Any help is appreciated. Edit: I do understand pointers before anyone asks. Just thought I would clarify. |
|
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
When you create a structure or a class in C++, you're creating a new type to go with it. Here, you're defining a new variable, structure, of type xampl. That variable has a sort of "subvariable", or member variable - x. When you're accessing these member variables, you use a dot:
structure.x = 5; std::cout << structure.x; You can also create a pointer to a structure, as you can see. However, the dot ( . ) operator takes precedence over the dereference operator ( * ). That means we have to use brackets: (*ptr).x; Because that's a bitch to type out, we use the "pointer member" operator: -> ptr->x; Hope that helped. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2005
Posts: 2
Rep Power: 0
![]() |
#include <iostream>
using namespace std; struct xampl { int x; }; www.geocities.com/donald_ferrone int main() { xampl structure; xampl *ptr; structure.x = 12;69696969696969 ptr = &structure; cout << ptr->x; cin.get(); } HOPE THIS HELPS. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0
![]() |
Yes, that definetely helped. Could you clarify what you mean by 'type' though? Hate to be a bother, but I feel that word is rather broad and I would love to understand it further. Other than that, you explained it much better than my tutorial did.
Edit: I forgot to mention what I thought by when you meant type, was that it was unique to that structure in a sense. Just doing my best to understand it. 8) |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Excellent question! A simple answer: type in this context means data type or representation. One differentiates between 'built-in' and 'user defined' data types. Examples of built in types are int, double and char. The struct you defined is of course a 'user defined' type.
Type Definition struct xampl
{
int x;
};After it has been defined, you may create instances of xample, just as you would create an instance of a built in type, such as int, double or char. As Ooble stated, in main you created an instance of type xample: xample structure; There is, of course, much more to be said about types, but it is probably best saved for later.
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Sep 26th, 2005 at 4:56 PM. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0
![]() |
Oh wow. Well you learn something new everyday!
Thanks for that clarification stevengs, it makes tons more sense now. You guys sure are a lot of help over here. 8) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|