I am new to C development and I am having an issue setting an arrays values in a function and returning to the calling method. The function itself has to return an int and the array size needs to be dynamic, so I am trying to update the original array utilising a pointer to the array. My code is as below:
int getArray(TestType *testArray)
{
testArray = malloc(2 * sizeof(TestType));
testArray[0].id = 1;
testArray[0].testFloat = 1.5;
testArray[1].id = 2;
testArray[1].testFloat = 2.5;
printf("getArray element 2 id = %d\n", testArray[1].id);
return 1;
}
void main()
{
TestType *testArray;
int i = getArray(*&testArray);
printf("main element 2 id = %d\n", testArray[1].id);
}
When I run this I get the following results:
getArray element 2 id = 2
main element 2 id = 0
I have looked elsewhere and although c returning an array from a function describes a similar issue this is dealing with a char array, whilst I have a user defined struct, so don't believe I can apply the same solution.