Link to home
Start Free TrialLog in
Avatar of sdj
sdj

asked on

struct within a struct?

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.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

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.
Avatar of sdj

ASKER

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