When using a dynamically growing array first discovered such a thing.
For () loop does not work at all.
Replacing to the loop while () corrected case.
Version of Pelles-C 5.0.0.8, a new not use for various reasons.
Threw everything I could to reduce the code snippet:
expand array dynamically
/* Struct for allocate in memory dinamic-growing array */
typedef struct
{
double x;
double y;
double z;
} Vertex;
typedef struct
{
size_t num_alloc;
size_t num_inuse;
Vertex *list;
} VertexList;
/* Functions for allocate in memory dinamic-growing array */
void initVertexList(VertexList *array)
{
// C99: *array = (VertexList){ 0, 0, 0 };
array->num_alloc = 0;
array->num_inuse = 0;
//array->list = 0;
array->list = NULL;
}
BOOL addPointerToArray(Vertex v1, VertexList *array)
{
if (array->num_inuse >= array->num_alloc)
{
size_t new_size = (array->num_alloc + 1);
Vertex *new_list = realloc(array->list, new_size * sizeof(Vertex));
array->num_alloc = new_size;
array->list = new_list;
}
array->list[array->num_inuse++] = v1;
return TRUE;
}
function Find3dPoly() may change name to main() - for() NOT works, but while() works fine
/* Calculate vertices of existing 3dPolyLine */
void Find3dPoly(void)
{
int i, EntType, nVers;
double vX, vY, vZ; // coords of point
char result[64];
// definitions for use dinaic growing array of vertex
VertexList array;
initVertexList(&array);
/*
vX =100.0; vY=50.0; vZ=25.0;
for (i==0; i<=12; i++)
{
vX +=.75; vY +=.50; vZ +=.25;
addPointerToArray((Vertex){ vX, vY, vZ }, &array);
sprintf (result, "X=%.3f Y=%.3f Z=%.3f", array.list[i].x, array.list[i].y, array.list[i].z);
MessageBox( NULL, result, TEXT("Find vertices for"), MB_OK);
}
for (i==0; i<=12; i++)
{
sprintf (result, "X=%.3f Y=%.3f Z=%.3f", array.list[i].x, array.list[i].y, array.list[i].z);
MessageBox( NULL, result, TEXT("Find vertices for"), MB_OK);
}
*/
i=0; vX =100.0; vY=50.0; vZ=25.0;
while (i<12)
{
vX +=0.75; vY +=0.50; vZ +=0.25;
addPointerToArray((Vertex){ vX, vY, vZ }, &array);
sprintf (result, "X=%.3f Y=%.3f Z=%.3f", array.list[i].x, array.list[i].y, array.list[i].z);
MessageBox( NULL, result, TEXT("Find vertices while"), MB_OK);
i++;
}
i=0;
while (i<12)
{
sprintf (result, "X=%.3f Y=%.3f Z=%.3f", array.list[i].x, array.list[i].y, array.list[i].z);
MessageBox( NULL, result, TEXT("Find vertices while"), MB_OK);
i++;
}
// ... ... ...
free(&array);
}