Solved

Pro*C structure of structures

Posted on 2004-09-27
6
831 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
#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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to recover a database from a user managed backup
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

763 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

12 Experts available now in Live!

Get 1:1 Help Now