Solved

struct within a struct?

Posted on 1998-11-02
4
380 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

815 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

9 Experts available now in Live!

Get 1:1 Help Now