Welcome! Log In Create A New Profile

Advanced

Coding: Dynamically Defined Variables

Posted by LordAshes 
Re: Coding: Dynamically Defined Variables
May 10, 2010 05:06PM
Quote
tueidj
Do you expect C to somehow magically know what your structs will look like without defining them?

Quote
WikiFSX
That's it. If you want C to know about all ~500 types, you are going to have to program support for all of them. This isn't some scripting language where you just automagically throw things in variables.

With all due respect, please READ the POST THREAD before responding...the two above answers are just a waste of posting space (no offense intended to the writers who are just trying to help) because the user obviously did not read the entire post thread. If they had they would not have written what they did since I am not trying to do either.

I stated numerous times that I WANT TO SPECIFY the structure to which the pointer points to. So this by definition means that I do NOT expect C to "magically know" and I am NOT "automatically throwing things in variables".

I have a pointer which can point to one of numerous structures. The decision as to which structure it will be will be defined at runtime (not design time). The issue isn't with defining the structures (yes, you will need to define each one of the structures). The issue is with having only one pointer that points to one of the structures (instead of having a pointer for each possible structure even if it is not used). Clear?

So in addition to assigning to the pointer the address of the structure (at runtime) I would like to TELL C which structure the pointer is pointing at (during runtime) so that the -> references work.

Having said all that I have a guess at how to do it...I just have to actually try it to see if it will work...

I think the trick is to do the case to one of the structures when you are using the structure as opposed to when you are assigning the structure. So, for example:

printf("Object 0: (%d,%d)",((StaticImageType)Objs[0].image_data)->width,((StaticImageType)Objs[0].image_data)->height);

I'll let you guys know how it works.
Re: Coding: Dynamically Defined Variables
May 10, 2010 08:56PM
Still haven't read how a union works, eh?
Re: Coding: Dynamically Defined Variables
May 11, 2010 02:04PM
Weird...I posted last to this thread but it says that it was last posted to by tueidj.
Re: Coding: Dynamically Defined Variables
May 11, 2010 02:58PM
Quote
LordAshes
Weird...I posted last to this thread but it says that it was last posted to by tueidj.
2nd page? Also, see the post above you.
Re: Coding: Dynamically Defined Variables
May 13, 2010 04:23PM
Thanks...I missed the second page link.

I will look up union and see if that will work for me. However by the sounds of it (if it is anything like a Mathmatical union) then I don't think this will be the solution...but I will check...I confess that I don't know anything about C/C++ unions so I will read up on them.

Thanks for the suggestion.

EDIT 1:

I just had a glance at Union and I am trying to understand them but I don't see the difference between a struct and a union. The few samples that I saw were used in similar ways to how I one would use struct and sometimes struct and union were being mixed. Can someone shed some light on the difference between a struct and a union.

EDIT 2:

WikiAnswer says that a Union is like a Struct except that all of the Union variables occupy the same memory address so that only one of them is used at a time.

So what you are suggesting is that I make my image_data a union of image_static and image_animated? Something like:

struct object_type
{
int x;
int y;
union
{
image_static_type: image_static;
image_animated_type: image_animated;
} image_data;
}

object_type obs[100];

So then I would access the individual structure data by something like:

objs[0].image_data.image_static->width

Is that correct?



Edited 3 time(s). Last edit at 05/13/2010 04:40PM by LordAshes.
Re: Coding: Dynamically Defined Variables
May 13, 2010 06:07PM
I already showed you how to use them and you shot me down: [forum.wiibrew.org]
Re: Coding: Dynamically Defined Variables
May 14, 2010 10:36PM
Quote
tueidj
I already showed you how to use them and you shot me down: [forum.wiibrew.org]

My appologies to you. I re-checked the code that you provided and it did indeed use union. When I first looked at this code I missed the union declaration and thus I thought that you where declaring a pointer to each possible structure...hence my response about not being practical (which you refer to as "shot me down"). When I re-checked it, I saw your use of the union and (having read up on it earlier) now I understand what you were trying to suggest.

It is not exactly the solution that I wanted (it still limits the code somewhat by having to have all the structures defined ahead of time) but I think I can make it work in my particular case.

Thanks for the input.
Re: Coding: Dynamically Defined Variables
May 26, 2010 10:25PM
I tried the cast on usage and I could not get it to work.

However, I tried the Union idea and it works great. It still means that I need to have a pointer to each structure in the union definition but that should be managable. Then, depending on which structure is needed, the correct structure pointer is initialized and used.

Thanks.
Sorry, only registered users may post in this forum.

Click here to login