I need to identify a global struct (array), consisted of 4 integers.
The problem is, size of that struct array is not known in advance.
I’m trying to make sth. like this:
typedef struct
{
int value;
int MAXleft;
int MAXright;
int MAX;
} strnum;
int main ()
{
int size;
scanf("%d", &size);
strnum numbers[size];
return 0;
}
I heard that, it is possible to do this by pointers but I don’t know how to do.
You can allocate the space for several structures dynamically like this:
Then you can use it like any regular array (mostly).
It might be more convenient to use
callocinstead ofmalloc. It allocates a number of blocks and fills them with zeros. Please note, thatmallocdoesn’t clear allocated memory.When you are done with the memory don’t forget to call
free( numbers ), which will return the allocated memory back to a memory manager.If you don’t
freeit when it’s no longer required and allocate some more and more, a memory footprint of the program will grow for no good reason as the program continues to work. This is called a memory leak and should be avoided. It might eventually result in the lack of memory for a program and unpredictable results.And don’t forget to include a
stdlib.hheader with prototypes of memory allocation functions.