element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
NexGen Flight Simuator Flight Simulator 101 or back to college - part 16: C Structures with in Structures
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: phoenixcomm
  • Date Created: 3 May 2023 5:34 PM Date Created
  • Views 1548 views
  • Likes 9 likes
  • Comments 12 comments
  • flight simulator
  • nexgen
  • back to college
  • c programing
  • flight simulation 101
  • data structures
  • struct as member of a struct
Related
Recommended

Flight Simulator 101 or back to college - part 16: C Structures with in Structures

phoenixcomm
phoenixcomm
3 May 2023

imageNow, Somewhere on the Stack Exchange, I heard some crud about you just can't have a struct as a member of a struct. Well, you sure can and now for all the curious minds out there I will show you how to do it. Remember I work with flight simulation, so you always need to know where you are. This is called position, and it is made up of both a Latitude and a Longitude which are both structures of themselves, enclosed in an outer structure. 

First I need to create two structs called Lat and Lon and I will make them out of a structure that I call DM, or Degrees and Minutes, and you also need to know the Hemisphere or hemi. 

struct DM {
	int degrees;
	float minutes;
	char hemi; 
	};

struct DM Lat;
struct DM Lon;
Now that I have that out of the way, I need to put this                   together and create my three structures: Current, Start, and Finish. You will note they all have a             structure type of Position. So you ask how do I access them? Well, you just use dotted notation.

struct Position{
	struct DM Lat;
	struct DM Lon; } Current, Start, Finish;
// Carswell: 32-46.151167N 097-26.492083W
	Start.Lon.degrees  = 32;
	Start.Lon.minutes  = 46.151167;
	Start.Lon.hemi     = 'N';
	Start.Lat.degrees  = 97;
	Start.Lat.minutes  = 26.492083;
	Start.Lat.hemi     = 'W';

Now, I hope I have helped some of you.. I think this will close this blog.  

  • Sign in to reply
  • phoenixcomm
    phoenixcomm over 2 years ago in reply to Andrew J

    I must have had a typo when I first put a struct in a struct. I use GCC and It works fine. -enjoy

    struct test {
    struct member {
    float x;
    float y;
    int degress; } angle
    int itemNumber;
    char discription[50];
    float weight; }TEST;

    I might have tried to declare an array char[50] description -LOL
    In my simulator, my aircraft model consists of a STRUCT with more than a few more STRUCTs in it. ie.:

    where things are measured from the tip of the tail forward, right wing to left, and the top of the tail to the ground.
    struct XYZ {
    float X;
    float Y;
    float Z;} XYZpart, XYZac, XYZmate;

    struct Locations {
    struct XYZpart;
    struct XYZac;
    struct XYZmate;}
    float partWeight;
    char partName[50];
    char partNumber[50];
    char atachName[50];
    char atachNumber[50];}Parts;
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • beacon_dave
    beacon_dave over 2 years ago in reply to colporteur

    Nesting of structures is covered pretty well in the short Microchip course:
    https://mu.microchip.com/advanced-c-programming

    Data Structures

    • Structures (14 min)
    • Unions (8 min)

    Lab1 - Nested Structures and Pointers to Unions and Structures (17 min)

    • In this section you will learn:
      • How to nest structures and unions
      • How to create pointers to unions and structures
      • How to use structure and union pointers

    One good thing about the lab is that it shows you how the data structure appears in memory:  

    image

    so in this case you can see the members of a union sharing the same address space, and the members of a nested struct using contiguous addresses.

    Sometimes the layers of abstraction can overly obfuscate an otherwise relatively simple concept. 

    But, as the saying goes, "you can lead a horse to water..."  Slight smile

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jc2048
    jc2048 over 2 years ago

    K&R edition 2 [the 'ANSI C' one] says: "Structures can be nested."

    I don't understand why you need to create instances of the first structure before incorporating it within the second.

    What are you using for a compiler? I've just tried the following simple program with VC++ 2010 and it compiled and ran fine.

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Andrew J
    Andrew J over 2 years ago in reply to phoenixcomm

    Interestingly, Smalltalk has no basic types, pretty much everything is an Object and thus has a Class definition (even Class is an instance of Metaclass, one of the few things that aren’t objects.  Most non-Smalltalk devs would declare it the Devil’s work because “not type safe” but that really wasn’t (isn’t) a problem.  I would hazard a guess that very little is developed in at language now but it was a really good language to use for OO development and when I transitioned to Java I used to curse it for having to deal with basic non-object types!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Andrew J
    Andrew J over 2 years ago in reply to phoenixcomm

    Ok; I’d like to say it makes sense but it doesn’t really - compilers often have their quirks.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube