-
struct question...
Hello.
Anyway, will someone tell me what this piece of
code does?
Code:
struct dastructure {
int abcd;
struct dastructure *next_abc;
}
That part that confuses me is the struct dastructure
*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.
-
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.
-
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.