0

I have struct:

typedef struct akcie
{
    int *stockArray;
} AKCIE;

Declaration

 AKCIE *idStock;

Malloc idStock

idStock = ( AKCIE *)malloc( id * sizeof( int )); // id is an integer

Now I want to malloc to every idStock[i], i = 1,2,3,..... stockArray in function, etc. void(parameters) { malloc every idStock[i].stockArray; }.

How to alloc idStock[0], idStock[1] in function? I dont know how to transmit the struct as parameter. Thank you for help.

0

3 Answers 3

1

Two mallocs are required. One for the AKCIE array and another for each int array. Below is an example for statically sized arrays:

#define STRUCT_ARRAY_DEPTH (10)
#define INT_ARRAY_DEPTH    (20)

int i;
AKCIE *idStock;

idStock = malloc(STRUCT_ARRAY_DEPTH * sizeof(*idStock));
for(i = 0; i < STRUCT_ARRAY_DEPTH; i++)
    idStock[i].stockArray = malloc(INT_ARRAY_DEPTH * sizeof(int));
Sign up to request clarification or add additional context in comments.

Comments

0

This is how you can alloc the memory for each of the elements in the struct.

This is the procedure:

void procedure(AKCIE *idStock, int n){ //n is the dim
        int i;
        for(i=0; i<n; i++){
          idStock[i].stockArray = (int *)malloc(HOW_MANY_DO_YOU_NEED * sizeof(int));
        }
    }

and this is how you call it:

    .
    .
    .
    procedure(idStock, dim);
    .
    .
    .

PS: there is an error in your allocation of memory for idStock. It does not contains int, but int *.

To avoid this kind of mistakes you can just pass the whole structure to the function sizeof().

1 Comment

Thank you, that is it, what i need.
0

This is incorrect:

idStock = ( AKCIE *)malloc( id * sizeof( int ));

Assuming idStock is to be treated as an array, and id is the length of that array, you're not allocating the proper amount of space. You're only allocating space for a number of int variables, but the struct contains an int *, which may not be the same size.

What you want is this:

idStock = ( AKCIE *)malloc( id * sizeof( AKCIE ));

This allocates an array of AKCIEs of length id. Once you do this, you don't need to allocate the individual array members, since it's already done.

4 Comments

Ok, you are right. It means, if id = 1, sizeof( AKCIE ) is 8, idStock will be idStock[0] ..... idStock[7]?
@Levin Not sure what you mean by that. Are you suggesting that sizeof(AKCIE) is dependent on the value of id? Because that's not true. The value of sizeof(AKCIE) is a constant.
I mean, it creat id * sizeof(AKCIE) places in array. For id = 1, it creat 8 places.
@Levin No, it doesn't create 8 places. It creates a space of 8 bytes, which is big enough for a 1 element array. So idStock[0] would be the only valid element in that case. If you want 8 elements then set id to 8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.