Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 26th, 2005, 2:40 PM   #1
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
A question about structures

Hey again. I had a question about some code I was trying out.

Quote:
#include <iostream>

using namespace std;

struct xampl {
int x;
};


int main()
{
xampl structure;
xampl *ptr;


structure.x = 12;
ptr = &structure;

cout << ptr->x;

cin.get();
}
Now I understand that the code in italics is defining the structure, but I'm not -entirely- sure what the code in bold is meaning. The tutorial is fairly vague and I would like to. My best guest would be...

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.
White-Hat` is offline   Reply With Quote
Old Sep 26th, 2005, 3:00 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Sep 26th, 2005, 3:22 PM   #3
Diamond_Don
Newbie
 
Diamond_Don's Avatar
 
Join Date: Sep 2005
Posts: 2
Rep Power: 0 Diamond_Don is on a distinguished road
#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.
Diamond_Don is offline   Reply With Quote
Old Sep 26th, 2005, 4:27 PM   #4
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
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)
White-Hat` is offline   Reply With Quote
Old Sep 26th, 2005, 4:38 PM   #5
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
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;
};
The name of the type is xampl, and it is defined as struct with an integer member.

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;
The variable, named 'structure', represents an instance of type xample.

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.
stevengs is offline   Reply With Quote
Old Sep 26th, 2005, 4:42 PM   #6
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
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)
White-Hat` is offline   Reply With Quote
Old Sep 27th, 2005, 12:23 PM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Glad to be of service.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:18 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC