To preface: I am new to C and still learning the best way to efficiently code in C.
I'm trying to create an dynamic array of structs. I have an array within the struct however the values within the array are being overwritten by the values that come after it.
struct dir {
int size;
int *parameters;
};
Here I am placing all values I read from the file into a temporary char array called vars2. I cross check the index of the string with a 2D vars array of strings to find the index of the value. If the index is not found I copy the value into the 2D vars array so an index can be assigned to it if later instructions calls for the new variable. length is the length/size of the 2D array.
I then assign the node to an index, reallocate memory for the next node, and move on to read the next set of instructions in the file.
The struct array is dependent on the number of instructions within the file. The number of instructions is unknown until we reach the end of file, that is why I do not increment it until the end of the loop. I have tried to increment it at the beginning of the loop, but it did not make a difference.
int numinstruct = 0;
struct dir *gates = malloc(1*sizeof(struct dir));
while (fscanf(fp, "%s", gate) == 1) {
struct dir node;
// code that reads from file are in between that assigns the struct size and type of gate
// node.size is the size of the array that was read from the file; parameters array is a static array
node.parameters[node.size];
for (i = 0; i < node.size; i++) {
fscanf(fp, " %16s", vars2);
int index = indexLookup(vars, vars2, length);
if (index != -1) {
node.parameters[i] = index;
} else {
vars = realloc(vars, (length+1)*sizeof(char*));
vars[length] = malloc(17*sizeof(char));
strcpy(vars[length], vars2);
node.parameters[i] = length;
length++;
}
}
gates[numinstruct] = node;
numinstruct++;
gates = realloc(gates, (numinstruct+1)*sizeof(struct dir));
}
All data are placed within the actual node and not the array itself. I have tried to place the data within the array itself, as seen below. However, the program would stop/crash once it got to the parameters section of the code.
typedef struct {
int size;
int *parameters;
} DIR;
DIR *gate = malloc(1*sizeof*gate);
// code in between
int numinstruct = 0;
struct dir *gates = malloc(1*sizeof(struct dir));
while (fscanf(fp, "%s", gate) == 1) {
struct dir node;
// code that reads from file are in between that assigns the struct size and type of gate
// node.size is the size of the array that was read from the file; parameters array is a static array
gate[numinstruct].parameters[ gate[numinstruct].size];
for (i = 0; i < gate[numinstruct].size; i++) {
fscanf(fp, " %16s", vars2);
int index = indexLookup(vars, vars2, length);
if (index != -1) {
gate[numinstruct].parameters[i] = index;
} else {
vars = realloc(vars, (length+1)*sizeof(char*));
vars[length] = malloc(17*sizeof(char));
strcpy(vars[length], vars2);
gate[numinstruct].parameters[i] = length;
length++;
}
}
numinstruct++;
gates = realloc(gates, (numinstruct+1)*sizeof*gates);
}
The thing I find most confusing is that the size variable does not get overwritten. The only values that get overwritten are the values within the parameters array. There isn't any code written before it that touches the parameter array, only the segment that I have provided which fills the parameter array with values.
For instance:
instruction 1: size = 2; param = {2, 3, 5}; instruction 2: size = 3; param = {3, 6, 8};
Once I read the instructions and print them out, outside of the loop I get the following:
instruction 1: size = 2; param = {3, 6, 8}; instruction 2: size = 3; param = {3, 6, 8};