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 1550 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 stead of this:

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

    struct DM Lat;
    struct DM Lon;

    I most likely could have done this:

    struct DM {
    int degrees;
    float minutes;
    char hemi;
    }  Lat, Lon;

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

     colporteur C is a very powerful language.  So pick dah book up and learn it.  There's stuff in the language that I still don't understand and hate that why my RSS is now written in JAVA as I don't have to worry about the C interrupt problems. It is very similar to Python (which I don't even use), but alas it does not use the semicolon to end a statement. The use of intentions for grouping instead of {} makes no sense to me at all.. It sort of reminds me a little of BASIC, FORTRAN, and Perl ( Perl is what I use for my website majic  but the $ before variable names makes me sick. and by the way, its also has no basic types such as int or float but I tops it when it comes to string handling. and btw it is Practical Extraction and Reporting Language

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

     /user/conversations?New=True&UserId=188728 Andrew, I actually tried that but the compiler did not like it. So do it the long way define the struct then stick it in the outside struct. hence a struct as a member of the struct.  così va

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

    An elegant solution.

    I am not surprised that you can reference a structure within a structure.

    The C language was always about building large data structures from smaller data structures.

    As long as all of the elements are correctly defined, the compiler just does not care.

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

    I’m not an experienced C or C++ programmer so please bear with a probably simple question: why do you have to declare Lat and Lon as DM types on the first code block lines 7 and 8, and then again within Struct Position on lines 2 and 3 of the second code block?

    • 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