Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

struct within a struct?

Posted on 1998-11-02
4
Medium Priority
?
388 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 400 total points
ID: 1176810
>>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
ID: 1176811
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
ID: 1176812
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
ID: 1176813
>> 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

916 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