I am facing a problem with storing arrays in structures, which are further written onto a file.
- The arrays are square matrices.
- The arrays and the size n are stored in a structure. This structure is then stored in a file(binary file).
- The size of this array is subject to input from the user during the run time.
- Whenever I read the structure from the file in the future, I want to recover all those elements which were saved as they were(I suspect that is why saving the array as int **a has not worked).
The problem is that the size of matrix can be variable and is subject to user's choice(which is, by the way, also stored in the structure and further onto file). I acknowledge that I can take the size of array to be large enough to accommodate any possible value of size, but that would be a wastage of space. Further if I write every element of the array individually then it would be rather a hassle to handle each value individually. Reading a structure is better than reading many values individually. Of course, if I do this:
struct attribs
{
int a[n][n], b[n][n], n;
}
then it doesn't work. Is there a smarter way around my problem? As for present I take a[][] and b[][] to be of size 20X20 each, while expected value is 4X4 to 9X9, and store the actual value in n so that only that part can be read easily when the structure is read.