Good afternoon everyone,
I need a struct where its internal fields are dynamically allocated and the number of records is also dynamically allocated.
My question is that I have no idea how it is possible to implement this!
Imagine the struct below, each field will have a different size and will vary according to the input text.
Example: Field1 can receive 1000 bytes or 3000 bytes
The number of records in the struct will start at 0, and will increase as a record is registered.
Code:
struct {
char
Field1[ 20],
Field2[ 20],
Field3[ 1001],
Field4[ 1001],
Field5[ 1001],
Field6[30000];
} Fields[50000];
I know how to use malloc to increase each field.
Code:
Field1 = malloc(101);
But how do I increase the number of records in the struct?
I also don't know if I can have for example different sizes in each record, since in a simple struct of char field[100] this is true for all records.
Record 1 = Field1 = 100 bytes
Record 2 = Field1 = 600 bytes
Can anyone help?