size vector in c

Hello,

I'm trying to create in C something like this:

1
2
3
4
5
6
7
struct ne{
    uint64_t shw;
    uint16_t th;
};

struct ne *ret[];


It gives error saying that the ret size is missing.
I do not declare the size, because the size is not fixed to a value, I want dynamic.

Is there other best way to do this?

Regards,
struct ne* *ret; a `dynamic array' of pointers.
struct ne *ret; a `dynamic array' of `ne' objects.

(spaces added for clarity)
Are you trying create an array of pointers to ne? In that case you can use a pointer to a pointer:
struct ne **ret;
To allocate the array of size n you can do.
ret = malloc(sizeof(struct ne*) * n);
To allocate each element i=0,...,n-1 you can do
ret[i] = malloc(sizeof(struct ne));

If you instead want an array of ret you can do
struct ne *ret;.
To allocate the array of size n you can do.
ret = malloc(sizeof(struct ne) * n);
Last edited on
First thanks for the tips,

I want a dynamic array of 'ne' objects.
I was thinking I have to put the [] after the struct ne *ret. @ne555 so its not necessary?
Last edited on
If you have struct ne *ret then ret can be used to to point to the first element in an array. The size of that array doesn't matter because ret is only pointing to one of the elements in the array.
1
2
3
4
struct ne{
    uint64_t shw;
    uint16_t th;
};


To declare
struct ne* ret;

To size:
ret = new ne[size];

I hope that's C anyways, I am more used to C++.
@Stewbond
C doesn't have new.
Dang, well then I'm out. I suppose in C memory is allocated during initialization. Wouldn't that make dynamic memory impossible?
^ man malloc
Topic archived. No new replies allowed.