| CMarco (46) | |||
|
Hello, I'm trying to create in C something like this:
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, | |||
|
|
|||
| ne555 (4042) | |
struct ne* *ret; a `dynamic array' of pointers.struct ne *ret; a `dynamic array' of `ne' objects.(spaces added for clarity) | |
|
|
|
| Peter87 (3691) | |
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
|
|
| CMarco (46) | |
|
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
|
|
| Peter87 (3691) | |
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.
| |
|
|
|
| Stewbond (1676) | |||
To declare struct ne* ret;To size: ret = new ne[size];I hope that's C anyways, I am more used to C++. | |||
|
|
|||
| Peter87 (3691) | |
|
@Stewbond C doesn't have new.
| |
|
|
|
| Stewbond (1676) | |
| Dang, well then I'm out. I suppose in C memory is allocated during initialization. Wouldn't that make dynamic memory impossible? | |
|
|
|
| ne555 (4042) | |
| ^ man malloc | |
|
|
|