Solved

struct within a struct

Posted on 2010-11-29
6
618 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

622 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