NO

Author Topic: for() not works, but while() works fine  (Read 2502 times)

sergey

  • Guest
for() not works, but while() works fine
« on: April 13, 2013, 09:26:45 AM »
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
Code: [Select]
/* 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
Code: [Select]
/* 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);
}

laurro

  • Guest
Re: for() not works, but while() works fine
« Reply #1 on: April 13, 2013, 10:45:13 AM »
"for (i==0; i<=12; i++)" it should be "for (i=0; i<=12; i++)"
so, this is not a bug.

Laur

czerny

  • Guest
Re: for() not works, but while() works fine
« Reply #2 on: April 13, 2013, 05:33:46 PM »
Your for loops are running from 0 to 12. Your while loops run from 0..11.

sergey

  • Guest
Re: for() not works, but while() works fine
« Reply #3 on: April 25, 2013, 09:06:48 PM »
Thank you, guys.
Now, I saw my mistake.