Solved

Pro*C structure of structures

Posted on 2004-09-27
6
878 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
[X]
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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
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 …
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
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.

724 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