Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

struct within a struct

Posted on 2010-11-29
6
Medium Priority
?
630 Views
Last Modified: 2012-05-10
hi there,

i came across this struct code and i noticed that it is declaring itself inside it owns struct.

for example:
i have a struct called abc. and inside this abc struct, there is a declaration of struct abc with a next pointer pointing to it.

struct abc {
        char a[20];
        long b;
        struct abc * next;
}

Open in new window


may i know how is this possible and what is the purpose for this?

thanks in advance.
0
Comment
Question by:gagajanice
6 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 34235670
This is not actually a struct within a struct (or nested struct). next is just a pointer to a struct abc. One common application for this is a single linked list. See, for example, the code in this question:
   http://www.experts-exchange.com/Programming/Languages/C/Q_23404167.html
0
 
LVL 15

Assisted Solution

by:DonConsolio
DonConsolio earned 200 total points
ID: 34235672
"next" is a pointer to a "struct abc" object.

The compiler knows the size and behavior of a pointer and can
insert it without knowing all the internals of the "abc" struct.
0
 
LVL 32

Accepted Solution

by:
phoffric earned 600 total points
ID: 34235685
Here are short tutorials on single linked lists:
    http://richardbowles.tripod.com/cpp/linklist/linklist.htm
    http://www.cprogramming.com/tutorial/lesson15.html

Notice that the code has the pointer to the struct.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Assisted Solution

by:themrrobert
themrrobert earned 200 total points
ID: 34235783
The reason is so that it can be a linked list, so that you can iterate through easily by changing the ref after your done using it.


i believe this is the best simplest answer to your question
struct* myStruct;

while(struct->next!=null) {
//do stuff
struct = struct->next; //automatically moves to next item
}

Open in new window

0
 

Author Closing Comment

by:gagajanice
ID: 34236080
thank you everyone for the explaination.

phoffric, thanks for the url. very useful to me.
0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 34236101
if you try using this:

struct abc {
        char a[20];
        long b;
        struct abc  next; //<-----without the pointer (*)
}

 you will get an error: 'next' uses undefined struct 'abc' since it is not declared.

Have a look at nested structs:
http://www.exforsys.com/tutorials/c-plus-plus/structure-in-c-part-ii.html
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month12 days, 16 hours left to enroll

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question