Solved

struct within a struct

Posted on 2010-11-29
6
609 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 50 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 150 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 13

Assisted Solution

by:themrrobert
themrrobert earned 50 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:DeepuAbrahamK
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

831 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