Solved

# structures

Posted on 2006-05-10
257 Views
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
Question by:mhho

LVL 8

Expert Comment

Array of pointers to structures of different sizes
0

LVL 8

Expert Comment

Let's say

void *array[10];

array[0] = new Object1;
array[1] = new Object2;
0

LVL 30

Expert Comment

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

LVL 15

Expert Comment

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

LVL 53

Expert Comment

>> 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

LVL 15

Expert Comment

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

LVL 5

Expert Comment

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

LVL 53

Expert Comment

>> 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

LVL 1

Accepted Solution

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticallâ€¦
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the bâ€¦
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.