Solved

Pro*C structure of structures

Posted on 2004-09-27
6
847 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

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.

Question has a verified solution.

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

Suggested Solutions

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…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
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.

770 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