Solved

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

Posted on 2003-03-11
Medium Priority
189 Views
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
Question by:CBMLude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 8

Expert Comment

ID: 8117273
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

LVL 8

Accepted Solution

akshayxx earned 80 total points
ID: 8117303
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

LVL 24

Expert Comment

ID: 8117306
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

LVL 8

Expert Comment

ID: 8117330
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

Expert Comment

ID: 8117353
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

Author Comment

ID: 8117364
WOW quickest response ever....
0

LVL 8

Expert Comment

ID: 8117461
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month10 days, 1 hour left to enroll