🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.c.moderated
1 messages
1 total messages Started by R Ghosh-Roy Thu, 11 Apr 1996 00:54
How to use sizeof and offsetof in structure arrays and its fields?
#2570
Author: R Ghosh-Roy
Date: Thu, 11 Apr 1996 00:54
123 lines
4469 bytes

Hello Everybody,

I am a bit confused about "pointer arithmatic" and therefore would appreciate
your expert help. The following is the problem:

I am defining a schema (or templates) using the following structure
definitions:

typedef struct SchemaStruc
{
  char *name ;
 
  List entities ;
} ;
 
typedef struct EntityStruc
{
  int id ;
  char *name, description ;
  long int strucsize ;
 
  List attributes ;
} ;
 
typedef struct AttributeStruc
{
  int id ;
  char *name, description ;
  long int fieldoffset ;
} ;
 

                /                  /
schema --------|-- entity --------|-- attribute
                \                  \
       1-many             1-many

I have defined three entities A,B and C. For example, entity A defines
a house and one creates lots of instances of A stored in an array. The
instances of B (people) and C (address) are also stored in arrays.

|--|
|  |-->A1->A2->A3->A4->etc             (instances of entity A)
|  |
|  |-->B1->B2->B3->B4->B5->B6->B7->etc (instances of entity B)
|  |
|  |-->C1->C2->C3->C4->C5->etc         (instances of entity C)
|--|


Each entity (say of size X using "sizeof") has many attributes and each 
attribute comes with an offset length (say Y using "offsetof") within the 
entity. I can then search for any attribute instance within an array of 
entity instances just by doing pointer arithmatic:

               entity Z
          -    |--------|  -
          |    |        |  |
          |    |        |  Y offset
          |    |        |  |
     size X    |        |  |
          |    | Za     |  -
          |    |        |
          -    |--------|


    /** Get X (sizeof) from the chosen entity definition **/
    /** Get Y (offsetof) from the chosen entity-attribute definition **/

  for (i=0; i < number_of_instances_in_an_array_of_structure_Z; ++i)
  {
    (i*X) + Y points directly to the attribute value I am after.
  }

This avoids me writing accessing function for each attribute. The program 
looks at the entity structure size and the field offset at the meta level 
and then uses pointer arithmatic to traverse the entity instances and its 
attribute instances. The beauty is that as its the same for all attributes,
I dont need individual function for accessing each attribute definition.


However, I now wish to add ANOTHER attribute which points to other entities.

      entity A
      |--------|
. . . |        |  . . . . . . . . . . . . . . . . 
      |        |
      |        |
      |        |        entity B
      | Ab  ---|------->|--------|
      |        |        |        |
      |--------|        |        | 
. . . . . . . . . . . . |        | . . . . . . . . . . . . . . . . 
                        |        |
                        |        |         entity C
                        | Bc  ---|-------->|----------|
                        |        |         |          |
                        |        |         |          |
                        |        |         |          |
                        |--------|         |          |
                                           |          |
                                           |  Cd      |
                                           |          |
 . . . . . . . . . . . . . . . . . . . . . |          |  . . . . . . . . . . 
                                           |          |
                                           |          |
                                           |----------|

WARNING: The solution to the following question is NOT trivial.
My question is: can I use sizeof and offsetof to get to Cd as I 
can do to get to Ab (described above)? Ie, *NOT* write specific 
functions for each attribute (Cd) to access its value by traversing 
down from entity A, but use only sizeof and offsetof.

Thanks,

Rana


-- 
R. Ghosh-Roy @ BIPS  -- R.Ghosh-Roy@brunel.ac.uk -- Extension 2772
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads