Solved

Pro*C structure of structures

Posted on 2004-09-27
6
841 Views
Last Modified: 2012-08-13
In pro*c (or ansi C also - I guess its the same answer) can I have a structure of structures.

Something like this:

typedef struct outer_
{
   int       error;                  /* was there an error? TRUE/FALSE                       */
   char      debug[12];              /* Placeholder for debug code                           */
   int       intdebug;               /* Placeholder for integer debug code                   */

   char      CustomerRef[STR20];
   char      ActualBillDate[STR20];

   typedef struct inner_
   {
      int       error;                  /* was there an error? TRUE/FALSE                       */
      char    debug[12];              /* Placeholder for debug code                           */
      int       intdebug;               /* Placeholder for integer debug code                   */

      char      AccountNum[11+1];   /* Account Number                                       */
      char      InvoiceNum[STR20];   /* Invoice number to which a payment has been allocated */
      char      PaymentDate[STR20];  /* Date of Payment                                      */
      int        PaymentAmount[1];       /* Amount of Payment                                    */
   } inner_t;

  } outer_t;

If so, how do I reference the inner elements?
If not, is there another way?

Julian
0
Comment
Question by:stummj
  • 2
6 Comments
 
LVL 22

Expert Comment

by:earth man2
ID: 12165187
this is why C++ introduces inheritance.  easy to do in c++ use class instead of struct.

typedef struct a {int x} a_t;
typedef struct b { char y[25]; a_t z; } b_t;

b_t h;

printf( "%d\n", h.z.x );
0
 

Author Comment

by:stummj
ID: 12167284
I'm not being facicious, but I'm not clear whether you are saying it cant be done in C or Pro*C or you dont know or what?
0
 
LVL 9

Accepted Solution

by:
pratikroy earned 250 total points
ID: 12167410
Declare the inner struct as :

typedef struct inner_
{
      int       error;                  /* was there an error? TRUE/FALSE                       */
      char    debug[12];              /* Placeholder for debug code                           */
      int       intdebug;               /* Placeholder for integer debug code                   */

      char      AccountNum[11+1];   /* Account Number                                       */
      char      InvoiceNum[STR20];   /* Invoice number to which a payment has been allocated */
      char      PaymentDate[STR20];  /* Date of Payment                                      */
      int        PaymentAmount[1];       /* Amount of Payment                                    */
} inner_t;

Then have an attribute in the outer struct (with the type of the struct above) :
typedef struct outer_
{
   int       error;                  /* was there an error? TRUE/FALSE                       */
   char      debug[12];              /* Placeholder for debug code                           */
   int       intdebug;               /* Placeholder for integer debug code                   */

   char      CustomerRef[STR20];
   char      ActualBillDate[STR20];

   inner_t * inner_var;

} outer_t;

You will declare a variable as :

outer_t        *outer_buffer;

And refer to the inner item by referring to :
outer_buffer->inner_var.error

Hope this helps.


0
 
LVL 22

Assisted Solution

by:earth man2
earth man2 earned 250 total points
ID: 12167705
#include <stdio.h>
#include <stdlib.h>
typedef struct a {int x;} a_t;
typedef struct b { char y[25]; a_t z; } b_t;

int main ( int argc, char *argv[] ){
b_t h;
h.z.x = 3145926;
printf( "%d\n", h.z.x );
exit(0);
}

you define two typedefs.  The second struct b_t contains struct member z which is of type struct a_t.
To get at struct a_t member x you must use x.  So if we define variable h, x is denoted by h.z.x

Using c++ you can define class that inherits other class(es).  In that way you don't need that annoying z intermediate reference to z.
#include <stdio.h>
#include <stdlib.h>
class a { public: int x; };
class b : a { char y[25]; public: a::x; };
int main ( int argc, char *argv[] ){
b h;
h.x = 2718;
printf( "%d\n", h.x );
exit(0);
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

896 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

13 Experts available now in Live!

Get 1:1 Help Now