Solved

struct within a struct?

Posted on 1998-11-02
4
378 Views
Last Modified: 2010-04-02
Is it valid to create a struct within a struct as below and then access the data directly. See example of implementation below definition.

/************************************************************************************
// DRIVER_INFO
// -----------
//
// Structure to encapsulate information required just by the driver and
// the scheduler.
//
 ************************************************************************************/
typedef struct driver_info
{
      struct driver
      {
            int buffer_id;

            bool process_image;
            UINT height;
            UINT width;
            PIXEL_DATA_FORMAT image_format;
            VIDEO_FORMAT video_format;
      } driver;

      struct scheduler
      {
            std::list<information_buffer*> results_list;
            std::list<information_buffer*> free_buffer_pool;
      } scheduler;

} driver_info;

main()
{
 driver_info data;
 data.driver.height = 100;
 ASSERT(data.driver.height == 100);
}

This compiles but when I attempt to debug I can not see the variables within the inner struct. I can set them and read them in code. My alternative is to convert the struct to a typedef and instantiate an instance of it:

typedef struct driver_struct
{
 UINT height;

 etx, etc
} driver_struct;

driver_struct driver;

This also works and allows access to the inner struct as well as displaying the instantiated variables in the debugger.

I am using VC6.0.
0
Comment
Question by:sdj
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
Comment Utility
>>Is it valid to create a struct within a struct as below and then access the data directly
Yes it is valid.  If it weren't valid it wouldn't compile.

continues
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I would suggest the following changes, they should allow you to see the members inside the structure.  

The "typedef stuct" syntax is not needed in C++ (like it was in C).  Not is the "struct" before you declare an instance of a structure.  

Don't create anonymous structure types.  That is structure that have a single instance and not specified type name.

This cleans up the stucture to.

struct driver_info
{
   struct driverType // Now it isn't anonymous.
   {
      int buffer_id;

      bool process_image;
      UINT height;
      UINT width;
      PIXEL_DATA_FORMAT image_format;
     VIDEO_FORMAT video_format;
   }
  driverType driver;  // Declare driver using the driverType structure  type/

   struct schedulerType // again not annonymous.
   {
      std::list<information_buffer*> results_list;
      std::list<information_buffer*> free_buffer_pool;
   }
   schedulerType scheduler;  // Again used declared type.

};  // don't need type here.

I think this will clean up your problems.  Let me know if you have questions.
0
 

Author Comment

by:sdj
Comment Utility
I have just tried doing the following:

struct driver_info
{
   struct driverType // Now it isn't anonymous.
   {
      int buffer_id;

      bool process_image;
      UINT height;
      UINT width;
      PIXEL_DATA_FORMAT image_format;
     VIDEO_FORMAT video_format;
   } driver;  // Declare driver using the driverType structure  type/

This also works, I assume it is the same thing? I.E. define the struct driverType and declare one called driver, as this works in the debugger. It appears the debugger does not like it if the declared type has the same name as the struct.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> It appears the debugger does not like it if
>> the declared type has the same name as the struct
That is probably the real heart of the problem.  In C that was not a problem since the stucture name was always preceeded with a "struct" keyword.  C++ did away with that and it can lead to some ambiguity now if the structure type name and a object name are the same.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now