[comp.lang.c] An array in a structure Or an Array Of Structures

bytor@milton.u.washington.edu (Jill Patterson) (05/01/91)

 	I hope someone out on the net can help me with this.  What I have is
a group of nested structures that contain information like the following:

 			Cell 1,  Cell 2, ..., Cell n

 +--Main Struct--+
 |+-Sub  Struct-+|
 ||    Data     || --> array[0], array[1], ..., array[n-1]    ] 
 |+-------------+|
 |+-Sub  Struct-+|   
 ||    Data     || --> array[0], array[1], ..., array[n-1]  
 |+-------------+|     
 +---------------+

 should i declare it like the following.

struct SubA
{
	int Array[n];
}

struct SubB
{
	int Array[n];
}

struct Main
{
	SubA	sub_a;
	SubB	sub_b;
};

or like this

struct SubA
{
	int value;
}

struct SubB
{
	int value;
}

struct Main
{
	SubA 	sub_a;
	SubB	sub_b;
}

Main MyMain[n];

I would be interested in hearing your opinions on the differences between
the two methods, optimization, modifying code later, ect..

Thanx So Much
-Jill Patterson
	-Jill

bytor@milton.u.washington.edu
.

chip@osh3.OSHA.GOV (Chip Yamasaki) (05/01/91)

In <1991May1.005354.3100@milton.u.washington.edu> bytor@milton.u.washington.edu (Jill Patterson) writes:

>I would be interested in hearing your opinions on the differences between
>the two methods, optimization, modifying code later, ect..

I don't know about performance considerations, but I would code for
readability here.  I think the way to do that would be to consider the
way you are accessing them.  If you usually access them in pairs then
the large array of pairs would be more readable.  If they will be
accumulated in pairs, but accessed separately (in different parts or the
program or in different ways) I think the pair of arrays might (just
might) be more readable.

As a guess I would lean toward the array of pairs.

-- 
--
Charles "Chip" Yamasaki
chip@oshcomm.osha.gov

jan@root.co.uk (Joseph Nave) (05/03/91)

In <1991May01.030335.17188@osh3.OSHA.GOV> chip@osh3.OSHA.GOV (Chip Yamasaki) writes:

>In <1991May1.005354.3100@milton.u.washington.edu> bytor@milton.u.washington.edu (Jill Patterson) writes:

>>I would be interested in hearing your opinions on the differences between
>>the two methods, optimization, modifying code later, ect..

Using pointers will be faster

	Joe
-- 
"I opened Pandora's box, let the cat out of the bag and 
put the ball in their court"
	Hon. J. Hacker (The Ministry of Administrative Affairs)