Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

struct within a struct?

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 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