The majority of forums are now only available as archives, which means posting/editing is disabled.
The Anything and Everything forum is still open.
The Anything and Everything forum is still open.
structs help
|
|||
|
Rank: ? (39)
Member #: 14820 |
ok i m revising for exams and was wondering how to use struct! i have an example in front of me but can anyone explain in english what each line does, (not EACH Line but like important lines marked // i have also included what I THINK they do so please correct me too! )
thanks also the confusion is how does it know the address of each chunk, etc Code:
Notice:
If you notice this notice, you should also notice that this notice is not a notice!
|
||
|
|||
|
|||
|
Rank: ? (37)
Member #: 15158 |
Okay, I'm not sure that you even know what structures are used for. I'll explain to you, since you seem rather confused in this area. Structures are a way of grouping and organizing code to make it more organized, not to mention other reasons, like code reuse. Anyway, when you create a structure, like:
struct AccNode { //... }; This is telling the compiler that everything inside the { }; is part of the structure. So, now you must instantiate (create an instance of) AccNode, like in this line: AccNode *p_head = 0, *p_temp; This creates two instances of AccNode, both pointers. p_head and p_temp. So, now you can use anything inside AccNode's { }; using either instance. For example, p_head->p_next != p_temp->p_next But, I think what you were trying to do with structures was wrong. I think that you were trying to do stuff like AccNode = 2; That is not a proper way to use structures. You must first instantiate a structure, then you can use and modify it's contents on an instance - by - instance basis. I'm sorry that this post was short and choppy and probably still a bit confusing, I didn't have much time to write it. So, if you're still confused, I can clarify for you. |
||
|
|||
|
|||
|
Rank: ? (614)
Member #: 9832 |
To me this appears to be simple linked list (there are tutorials&thread about them). This means that every AccountNode contains the actual account data (the account struct) and the address of the next account in the list.
AccNode *p_head = 0, *p_temp; This line creates two pointers of type AccNode, but they point to nowhere right now. No instances of any struct yet. case 'A' p_head = new AccNode; //p head points to a new location p_head ->p_next = p_temp; First line makes p_head to point to same address (same struct) as p_temp. At the first time this appears to make no sense as both pointers point to nothing... second line finally creates something, a struct of AccNode type that actually contains one account struct. p_head now points to this struct. And next, the p_next member of the struct pointed by p_head is set to point p_temp (still zero) Now lets say the user creates a second node: p_temp is made to point to first node (not zero anymore), 2nd node is created, and the p_next of the 2nd node is made to point to the first node. This happens every time a node is created so 3rd points to 2nd and so on. In other words, linked list. cout<<"enter pin "; cin>>p_head->acc.pin; cout<<"enter balance "; cin>>p_head->acc.balance; cout<<"enter rate "; cin>>p_head->acc.rate; break; Remember that struct AccNode contains Account struct inside it? These lines of code insert some data into member variables of that Account struct. for example: cin>>p_head->acc.pin means in English: put user input into pin-variable of acc-struct inside AccNode struct pointed by p_head. "." is used to access members of a class/struct and "->" is used to access members of class/struct through a pointer. [(*p_head).acc.balance] would do the same thing I think. case 'P' while (p_temp != 0){ cout<<"PIN "<<p_temp->acc.pin<<'\n'; cout<<"Balance "<<p_temp->acc.balance<<'\n'; cout<<"Rate "<<p_temp->acc.rate<<'\n'; p_temp = p_temp->p_next; //ptemp is pnext this prints everything in the whole list, in other words every account. p_temp=p_head makes p_temp to point to the last created node, next lines print all data and p_temp = p_temp->p_next makes p_temp to point to next node (it kind of goes backwards from last to first...). while (p_temp != 0){ this checks if p_temp is zero, which would mean p_next of the node just printed was zero. In other words, when the last (well first actually) node is printed and there is nothing more, the loop stops. hope I didn't confuse you more...
Chaos reigns within - Reflect, repent, and reboot - Order shall return
|
||
|
|||
|
|||
|
Rank: ? (39)
Member #: 14820 |
thanks ever so much, but you mean tutorials on HERE or other sites?
why didnt I THINK OF THAT!! sorry! would have saved you a LOT of writing.
Notice:
If you notice this notice, you should also notice that this notice is not a notice!
|
||
|
|||
|
|||
|
Rank: ? (39)
Member #: 14820 |
case 'A'
p_head = new AccNode; //p head points to a new location p_head ->p_next = p_temp; the key is these lines then? and p_next is actually linked to each new struct instance?? like in my example i will have a pin, balance, rate AND a p_next which is pointing to the previous instance?? but what i m confused about is that where is it saved?? i mean for the first instance of structure will have a pointer to the next instance, but where will that instruction be saved? Also can you tell me what this means: struct AccNode{ Account acc; AccNode *p_next;
Notice:
If you notice this notice, you should also notice that this notice is not a notice!
|
||
|
|||
|
|||
|
Rank: ? (614)
Member #: 9832 |
Tutorials here is what I meant but there are tuts on other sites as well. And lots of stuff about linked lists in the c++ forum too. There's couple examples about how to do it with classes too. But you were interested in structs, not linked lists right?
Chaos reigns within - Reflect, repent, and reboot - Order shall return
|
||
|
|||
|
|||
|
Rank: ? (39)
Member #: 14820 |
Crypdoctor
But you were interested in structs, not linked lists right
yes i was interested in structs.
Notice:
If you notice this notice, you should also notice that this notice is not a notice!
|
||
|
|||
|
|||
|
Rank: ? (39)
Member #: 14820 |
case 'A'
p_head = new AccNode; //p head points to a new location p_head ->p_next = p_temp; the key is these lines then? and p_next is actually linked to each new struct instance?? like in my example i will have a pin, balance, rate AND a p_next which is pointing to the previous instance?? but what i m confused about is that where is it saved?? i mean for the first instance of structure will have a pointer to the next instance, but where will that instruction be saved? Also can you tell me what this means: struct AccNode{ Account acc; AccNode *p_next;
Notice:
If you notice this notice, you should also notice that this notice is not a notice!
|
||
|
|||
|
|||
|
Rank: ? (31)
Member #: 14393 |
loverboy260
case 'A'
p_head = new AccNode; //p head points to a new location p_head ->p_next = p_temp; the key is these lines then? The key would be these lines, they create what is called a linked list. p_head = new AccNode creates a new object of AccNode type in memory. and p_next is actually linked to each new struct instance?
No, p_next points to the instance created the last time around the loop. like in my example i will have a pin, balance, rate AND a p_next which is pointing to the previous instance?
Yes, unless this is the first time around which then p_next is 0. but what i m confused about is that where is it saved?
In heap memory. i mean for the first instance of structure will have a pointer to the next instance, but where will that instruction be saved?
It is not saved in any variable it is just in memory, that you can access through the pointers (p_next) of the AccNodes before it. Also can you tell me what this means:
struct AccNode{ Account acc; AccNode *p_next; This defines a user defined object, called a struct. This struct has two pieces of information a variable that stores an Account object, and a pointer that contains the location in memory where the next AccNode is located. Hope that helps! |
||
|
Please login or register to post a reply.
