So this is my struct in a header file:
struct _Variable {
char *variableName;
char *arrayOfElements;
int32_t address;
};
typedef struct _Variable Variable;
and here is my implementation of the init function in .c file:
void initVariable(Variable *variable, char *variableName, char *arrayOfElements,
int32_t address) {
int lengthOfVariableNameWithTerminatingChar = strlen(variableName) + 1;
variable->variableName = malloc(
sizeof(char) * lengthOfVariableNameWithTerminatingChar);
strncpy(variable->variableName, variableName,
lengthOfVariableNameWithTerminatingChar);
int lengthOfArrayOfElementsWithTerminatingChar = strlen(arrayOfElements)
+ 1;
variable->arrayOfElements = malloc(
sizeof(char) * lengthOfArrayOfElementsWithTerminatingChar);
strncpy(variable->arrayOfElements, arrayOfElements,
lengthOfArrayOfElementsWithTerminatingChar);
variable->address = address;
}
I get no errors when I compile but when I run my test file:
void test_initVariable() {
printf("\n---------------test_initVariable()-----------------\n");
// TODO:
Variable *variable1;
initVariable(variable1, "variable1", "1, 2, 3", 4); // <== Causes binary .exe file to not work
}
Can anyone tell me how to fix my implementation?