• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

It is possible to allocate a dynamic sized array in a struct?

I want to keep track of how things are initialized, therefore I need to know how to define an array structure with a varying size each time.

For example I want something like:

typedef struct{
int i;
int position[i];
int type;
} blargh;

but it will complain about the i not defined... any help?
0
CBMLude
Asked:
CBMLude
1 Solution
 
akshayxxCommented:
u better shud have this..
typedef struct{
int i;
int *position;
int type;
} blargh;

when u make a variable
blargh b1;

b1.position=NULL;
get the value for b1.i;
say b1.i=23;

then u can do
b1.position=(int*)realloc(b1.position,sizeof(int)*(b1.i+1));

now u can do
b1.position[i]=some value;
0
 
akshayxxCommented:
sorry i misunderstood ur question ...
u can have a function like this
blargh *newBlargh(int i){
blargh *tmp;
tmp=(blargh*)malloc(sizeof(blargh));
tmp->i=i;
tmp->position=(int *)malloc(i*sizeof(int));
return tmp;
}

and wherever u want new variable of type blargh initialised according to value of i..
do this
blargh *newB;
i=12;
newB=newBlargh(i);

and u can access the members like this
newB->position[3]=something;
newB->type=another number;
0
 
fridomCommented:
What you want to achieve does not work that way in C. You have to write (not tested!)

typedef struct my_dynamic_array{
    int size;
    int *position;
    int type;
} dynamic_array;

Than write  a "Creation" Routine

dynamic_array *  new_array(int requested_size){
    result = malloc (sizeof (*result));
/* and now you allocat the space you need for the array in the structure !*/
    result->positon = malloc(requested_size * sizeof(int));
}

for cleanung up write a sort of "Destructor", the answer to you queston in C is to allocate the memory dynamic.

Regards
Friedrich



   

 
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!

 
akshayxxCommented:
dont have much to do ... so here is a sample, how u wud like to use returned structure .. to ensure that indices that u use on position is within the range of is size

say newB=newB=newBlargh(i);

then u can do
int j;
for(j=0;j<newB->i;j++)
newB->position[j]=i*i;

newB->type=some_number_of_ur_choice;

hope this helps
0
 
MN_GManCommented:
CBMLude,

Try using a pointer in the structure and allocate space for the array after defining the structure variable.  


typedef struct {
   int i;
   int *position;
   int type;
}  blargh;

blargh      b;

b.i = (int) NumberOfArrayItems;
b.position = (int *)calloc(b.i, sizeof(int));

Hope that helps,
Gary
0
 
CBMLudeAuthor Commented:
WOW quickest response ever....
0
 
akshayxxCommented:
dont have much to do ... so here is a sample, how u wud like to use returned structure .. to ensure that indices that u use on position is within the range of is size

say newB=newB=newBlargh(i);

then u can do
int j;
for(j=0;j<newB->i;j++)
newB->position[j]=i*i;

newB->type=some_number_of_ur_choice;

hope this helps
0

Featured Post

Independent Software Vendors: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now