![]() |
| | #1 |
| Badly Drawn Boy Join Date: Aug 2001 Location: Salt Lake City, Utah
Posts: 1,201
| struct question... Anyway, will someone tell me what this piece of code does? Code: struct dastructure {
int abcd;
struct dastructure *next_abc;
}
*next_abc statement. What exactly is that doing? I know that *next_abc is a pointer, but why is the struct keyword used again? Thanks, static.
__________________ Staying away from General. |
| ethic is offline |
| | #2 |
| Registered User Join Date: Sep 2001
Posts: 752
| When using structires, you have to use the keyword struct every time you refer to a structure class. It's kinda an annoying feature of the language, *shrug*. So that's why you need to use the struct keyword within the struct. And for explaining why there's a pointer in the struct... that kinda struct that you posted looks to be a linked list node. Consider that we wanted to keep a set of integers, using the abcd field of this structure. Each structure would represent one integer... The first struct would contain the first value, and it's pointer would point to the second struct. The second struct would contain the second value, and it's pointer would point to the third struct. ... The last struct would contain the last value, and it's pointer would be NULL. So from the first struct, we can reach any value in the list by going from struct to struct. Of course, we'd have to keep a pointer to the first struct as well. It's the kind of thing where pictures help, but those aren't really available to me. Most any textbook should be able to illustrate the idea, and all the neat things one can do with linked lists that were impossible with arrays. |
| QuestionC is offline |
| | #3 |
| It's full of stars Join Date: Aug 2001
Posts: 4,833
| The reason is that a structure in C does not define a new data type, (this is a difference between C and C++ because in C++ it does). The name you give after a struct keyword is called it's tag, the tag by itself is not a type name. Thus when you want to create a variable that is a struct in C you must say it is a struct, then specify the tag to identify which kind of struct. The struct keyword is ignored by a C++ compiler when declaring variables or pointers to a struct.
__________________ Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream. |
| adrianxw is offline |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help with linked list sorting function | Jaggid1x | C Programming | 6 | 06-02-2009 02:14 AM |
| linked list question | brb9412 | C Programming | 16 | 01-04-2009 04:05 PM |
| Looking for a way to store listbox data | Welder | C Programming | 20 | 11-01-2007 11:48 PM |
| towers of hanoi problem | aik_21 | C Programming | 1 | 10-02-2004 01:34 PM |
| Passing pointers between functions | heygirls_uk | C Programming | 5 | 01-09-2004 06:58 PM |