• ### Popular Now

• 16
• 15
• 12
• 9
• 10

#### Archived

This topic is now archived and is closed to further replies.

# structs in structs in structs in structs in structs in ...

This topic is 6492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

For demonstrational purposes, let's say we have the following struct:

struct Happy
{
int X;
int Y;
int Z;
RECT AppleSauce;
}


Now let's say we define a PowerKeg as follows:
struct PowerKeg
{
Happy DeathlyIll;
int count;
}

// and then:

PowerKeg Zippy(100) // <- This is an array, but I can't user brackets because it thinks it's html coding...

Now that we have the formality out of the way, here is the real meat of the question. Let's say that I have some nice while loop...no, let's make that a for loop. So as we are transversing the loop as they say, we are doing something as follows:
for(int i = 0; i < 100 ; i++)
{
Zippy .DeathlyIll.AppleSource.left = 0;
Zippy .DeathlyIll.AppleSource.right = 1;
Zippy .DeathlyIll.AppleSource.top = 2;
Zippy .DeathlyIll.AppleSource.bottom = 3;
};

This would of course be pointless to do, but it seems to me that it would (should?) be possible to make this a little pretty (and assumedly faster) by doing something like the following:
RECT *SuperTemp = NULL;
for(int i = 0; i < 100 ; i++)
{
SuperTemp = &Zippy(i).DeathlyIll.AppleSource;
*SuperTemp.left = 0;
*SuperTemp.right = 1;
*SuperTemp.top = 2;
*SuperTemp.bottom = 3;
}

Well, something like that... Anyway, any answers or comments would be...well, most helpful ... Thanks, -> Briar LoDeran <- Edited by - BriarLoDeran on 6/14/00 11:33:09 PM

##### Share on other sites
In this part:

RECT *SuperTemp = NULL;
for(int i = 0; i < 100; i++){
SuperTemp = &Zippy.DeathlyIll.AppleSource;
*SuperTemp.left = 0;
*SuperTemp.right = 1;
*SuperTemp.top = 2;
*SuperTemp.bottom = 3;
}

don''t you need this:

RECT *SuperTemp = NULL;
for (int i = 0; i < 100; i++) {
SuperTemp = &Zippy[i].DeathlyIll.AppleSource;
*SuperTemp->left = 0;
*SuperTemp->right = 1;
*SuperTemp->top = 2;
*SuperTemp->bottom = 3;
}

???

--dron1

##### Share on other sites
Yes, you can do that, and if it helps readability, go for it.

It won''t really be any faster though. In either case, the CPU will access the data by way of offsets from a base pointer (in the first case, its hidden in the C array, but its there). You''re only making the offsets smaller when you make a direct pointer to your data. Anyways, on 386+ processors, indexed addressing is free, so there''s no penalty in any case.

- Remnant
- (Steve Schmitt)

##### Share on other sites
So, if I understand this right, aside from the readability of the code, there is no speed wise reason to do this, right? It seems to me that this MUST save you something, even if it is a mere processor operation. Well, thank Remnant!

-> Briar LoDeran <-

##### Share on other sites
Nup, saves nothing. You could however go:

struct Happy
{
int X;
int Y;
int Z;
RECT *AppleSauce;
}

RECT SuperTemp;
SuperTemp.left = 0;
SuperTemp.right = 1;
SuperTemp.top = 2;
SuperTemp.bottom = 3;

for(int i = 0; i < 100 ; i++)
{
Zippy(i).DeathlyIll.AppleSource = &SuperTemp;
}

Which would be you''d have to go and manually set each one to null when you lose the SuperTemp from the context, but until then, you can change them all by changing super temp.

Also quicker; since you only make 100 + 4 assignments, rather than your example, where you make: 100*(5) = 500 assignments...

Probably not worth doing; since you probably want an indervidual structure for each point the array; but hey. Nevermind.

You can always use the dreaded malloc to create a new Rect if you have to change the points, in which case it ends up being slower anyway... O well. *shrugs* Worth a thought.