You allocate memory, then you change the pointer
fig_point = malloc(sizeof(fig) * fig_size); // allocate here
assert(fig_point != NULL);
fig_point = &figure; // now you make fig_point point to something else
therefore your fig_point
pointer no longer points to your dynamically allocated memory. If you then do this
fig_point[n]
you are accessing memory out or range since figure
is not an array. In addition you pass the pointer fig_point
to create_FigArray
directly. This will create a copy of the pointer and hence any changes you make to that argument are in fact only changes to the copy
. This means that the dynamic memory you that the address stored in fig_array
after create_FigArray
has returned is the same as it was before - it is only the copy
that was changed by the function. If you want to change the pointer you need to use a double pointer argument to the function and then something like
void create_FigArray(fig** fig_point, int fig_size)
{
*fig_point = malloc(sizeof(fig) * fig_size);
}