PDA

View Full Version : Array of various type/struct pointers


LiquidG
10-23-2002, 04:57 AM
Brushing up on a little C, and care ot know if its possible to create an array of pointers such that each pointer is of a different type/struct. Any good references on this, or any good examples? The array will be of known length and will never need to be increased in size.

Hope this isn't too much of a n00b question :(

bwkaz
10-23-2002, 08:46 AM
You could make them all void pointers, that way they'd all be able to point to anything. But then you lose compiler type-checking...

If it was C++, you might be able to make the array contain pointers to a base class type, and make sure all the important functions are virtual. But it doesn't sound like you want to use C++, so... probably void * is the way to go, unless someone else knows of a good way to do it.

LiquidG
10-23-2002, 01:21 PM
Thanks for the help ... just to clarify, if i declare something like the following:

void *blah[10];

then if i wanted to go through and give each cell in the array and declare it to be of a different type/struct, would this work:

type1 myType;
blah[1]=myType;

sorry for being inquisitive, it seems real simple but i just want to make sure.

kmj
10-23-2002, 01:35 PM
you'd have to do

blah[1] = &mType;


because blah is an array of pointers, so you want to give it the address of your variable.

The real question is, later on, how you're going to know what type each element is.

bwkaz
10-23-2002, 01:43 PM
You'd have to use the address of myType, but it would work, yes:

blah[1] = &mytype;Well, wait a minute... let me make sure I understand what you were asking. Do you want to store a pointer to the "mytype" object into the array in this code? Or are you trying to tell the compiler that blah[1] points to a "type1"? Because there's no way to do the second one with an array. Arrays have to contain a whole bunch of the same "thing", by definition -- if they didn't, there'd be no way to do O(1) (constant-time) retrieval. Now that "thing" that they contain can be a generic pointer to some other type, but (and I don't know if this is what you're trying to get or not, but...) arrays don't and can't act like VB's Collection objects.

If you were to go to C++, you could make some generic base class for all your objects (call it Arrayable or something), and then make each type a subclass of Arrayable. Then make an array (actually, one of the STL collection objects might be better) of Arrayable pointers, and make all your functions virtual (actually, pure virtual, in this case, might be better...).

LiquidG
10-23-2002, 02:10 PM
Let me me go into a bit more detail ...

What I want is a fixed sized array, lets say of size n. I want it to contain a collection of pointers, but each pointer should be of a different type. The reason I want it this way is because I am retrieving data from different sources, and each will be a different struct.

With that in mind, is this still possible? From what I've read from everyone's post so far, I may have to reconsider this whole array deal and possiblly opt for linked lists perhaps (will those work even?)

Thanks

kmj
10-23-2002, 02:29 PM
LiquidG -> as bwkaz said, in C++ you can take advantage of inheritance to do what you're trying to do. (This is actually one major reason for inheritance and OOP). Is C++ an option?

In C or C++, from an array's perspective, all of it's elements are of the same type... that's all there is to it. Now, you *can* actually put pointers to different types in there, as we showed above, but the array will still think it's a void*.. btw, you may have to cast it to a void pointer to keep the compiler from complaining.. i.e:


blah[1] = (void*)&mytype;


Then to use it you'd have to do

(mytype*)(blah[i])->mytype_function();

LiquidG
10-23-2002, 03:31 PM
Thanks everyone ... C++ isn't really an option in this case, but I will try the suggestion with the void pointer ... i'll know what each cell of the array will contain, and will know its exact length.

Thanks again :)

(BTW, will the same problem exist with linked lists? ... and is there another ADT that i'm missing that would accomplish what i want other than an array?)

kmj
10-23-2002, 04:10 PM
If you know that element 1 is of type myType, you'll be find.

I don't know of any specific data structure that can handle this... some languages (like python) handle it implicitly, c++ as we mentioned handles it with inheritance...

You could do a linked list, but you'd still have to know which nodes were of what types, so you're not buying yourself anything... In fact, what you're doing isn't going to buy you a whole lot really.. what's your reason for keeping an array of pointers to these objects, if you obviously already have all the bookkeeping about which object is where?

LiquidG
10-23-2002, 04:20 PM
Here's my rational :)

My first cell will only contain a struct of a pair of doubles, my second cell an int, double, and pointer to a char, my third cell just a pair of ints, but my fourth cell will be a struct that contains a significant amount of info ... i know i can create one single struct that contains all of these items grouped into it and then only use the ones that are applicable to that array cell, but I was hoping to streamline things ... in addition, i'm transfering information wirelessly, and do to the nature of the transmission schedule need to transmit things that are only vital (since some parsing and sorting will need to be done on the receiving end).

Hope this helps.

stuka
10-23-2002, 07:00 PM
Is this a regular repetition over a fixed array in a predictable order? If so, there are probably better ways to structure the code than to use an array.

jamessan
10-23-2002, 08:02 PM
Why not use a struct of structs?

LiquidG
10-23-2002, 09:46 PM
Yes, the array length is known in advance, it will not vary ... in addition, I have predetermined what structs will be contained in each array, and only those will be contained in that particular array index. The purpose of constructin something in this fashion is because the data will be 'time-sensitive', ie the data contained in each cell will correspond to a particular time, and then be transmitted wirelessly for data collection purposes.

I do have some structs that are 'structs of structs' as you mention, but I dont see how this solves my initial problem ... care to expand on your previous comment please?

jamessan
10-23-2002, 10:49 PM
I was just thinking that since you know the length of the data structure (array as you suggest) and it will never change, you could just create a struct (outerStruct) that has a struct(innerStruct) for each cell you wanted in the array. This seems to fit your problem the best since structs are intended as a way to group related data structures that can be of varying data types.

sans-hubris
10-23-2002, 11:04 PM
Why don't you just use a union, and make an array of that union?

LiquidG
10-24-2002, 01:06 AM
I like the idea of the inner and outter struct, but the one thing that i should have mentioned is that the transmission might not always be every cell of the array ... each cell has a 'valid' flag, so if the data is not valid (wont go into determining that) it should not be sent to be transmitted (ie, via string concatenations) ... so with that in mind, would you still advise the inner and outter structs?

So, what's this bout a 'union' ... i am not familiar with how to do that (or is it as easy as i think) ...

kmj
10-24-2002, 01:42 AM
I still don't see why you have these in an array... I mean, why not just have 5(or however many) different objects? Why do you want to keep an array of pointers?

stuka
10-24-2002, 03:10 AM
kmj: that's kind of what I was hinting at - there's no need for an array here. Seems to me like the desire to create an array is making this a TON harder than it oughta be.

LiquidG
10-24-2002, 04:04 AM
Thank you all, the more I think about it, the more I agree with you all ... a struct that contains 'n' number of pointers to whatever I am trying to put into 'n' array cells will work much more nicely.

Thanks again!