Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

structures

I am wondering if you can have an array of structures then is it really an array? If arrays only hold similar items, then how does it hold structures which hold different items?
0
mhho
Asked:
mhho
  • 2
  • 2
  • 2
  • +3
1 Solution
 
Dariusz DziaraProgrammerCommented:
Array of pointers to structures of different sizes
0
 
Dariusz DziaraProgrammerCommented:
Let's say

void *array[10];

array[0] = new Object1;
array[1] = new Object2;
0
 
AxterCommented:
Hi mhho,
If you want to store different types, you would need to do it in an array of pointers, as in mrblue's example.

You can not store an array of different objects in a concrete type.

David Maisonave (Axter)
Cheers!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
efnCommented:
An array of structures is really an array.  All of the structures are similar items--they all have the same parts in the same order.  The parts may have different values in different array elements, but that is no different from having arrays of simple objects with different values.  If the structures are not all the same type and don't all have the same parts, you can't store them in an array.  That is, there is no such thing as an array of "struct", where an element can be any type of structure.
0
 
Infinity08Commented:
>> there is no such thing as an array of "struct", where an element can be any type of structure.
There is an array of union ...
0
 
efnCommented:
There is also no such thing as an array of "union," where an element can be any type of union.  Also, there is no such thing as a union of all conceivable types.  And I'm beginning to have doubts about unicorns.
0
 
gerhardschoemanCommented:
mhho,
an array is a list of items of the same TYPE, not necessarily of the same value. A struct is a type that groups other types. So for example:
struct A
{
int x;
double y;
};

It is true that int and double are different types but struct A now groups them. So if you now create an array of the struct (A *Aarray = new A[10]) then the array is all of the same type in other words: A

regards;
0
 
Infinity08Commented:
>> Also, there is no such thing as a union of all conceivable types.
Why do you need one for all conceivable types ? You just need one for those that are actually gonna be used in the array.

But, we're digressing from the subject ... :)
0
 
tushar_compCommented:
hi ,

 consider in this way
 if u have an arry of primitive type say int
 int a[10];
 this will eat up 40 bytes

 now with the structure
 like below
 
typedef struct test
{
  int i;
 int j;
 int k;
};

 here each element of type test will eat 12 bytes

 if u have arry of this
 
test a[10];
then u will eat 120 bytes of memoryy
 
 so structure is user type
 everything else will be same as primitive type
 its pointer
 each instance of structure is equivalent to each element of array of that type
 so it can have members of diff types
 so collection of structures (user type) which has same elements  <==> to collection of the  same data type  elements

 tushar
 

 
                         
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now