struct within a struct

Posted on 2010-11-29
Medium Priority
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.
Question by:gagajanice
LVL 33

Expert Comment

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:
LVL 15

Assisted Solution

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.
LVL 33

Accepted Solution

phoffric earned 600 total points
ID: 34235685
Here are short tutorials on single linked lists:

Notice that the code has the pointer to the struct.
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

LVL 13

Assisted Solution

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


Author Closing Comment

ID: 34236080
thank you everyone for the explaination.

phoffric, thanks for the url. very useful to me.
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:

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

  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 …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

607 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