Solved

struct within a struct

Posted on 2010-11-29
6
613 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
Industry Leaders: 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 79
How to split this in C++ 4 119
C++ question 3 74
Android development question 2 78
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

749 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