hi,
I am reading from a file which has 6 records(for now) and loading them into a structure. but when i try to print it back, it only prints the last record 6 times. it prints all 6 records right after loading it. but printing the whole structure later fails. i know theres a prob but am unable to figure out. any help will be appreciated. thanx in advance. here are some snippets of my code...
2 structures...
Code:
typedef struct Cars{
int id;
char make[SIZE];
char model[SIZE];
char color[SIZE];
int year;
long price;
CAR_TYPE type;
}Car;
typedef struct CarInventory{
Car **pList;
int maxSize;
int curSize;
}CarInv;
memory allocation
cInv.pList = (Car **)calloc(cInv.maxSize, sizeof(Car *));(in main)
Code:
BOOL bReallocateMemory(CarInv *cInv, Car *aCar)
{
if(cInv->maxSize <= cInv->curSize )
{
cInv->maxSize = (cInv->maxSize >= 1) ? cInv->maxSize * 2 : 1;
printf("Reallocating array size to %d", cInv->maxSize);
cInv->pList = (Car **)realloc(cInv->pList, cInv->maxSize * sizeof(Car *));
if(cInv->pList == NULL)
{
printf("realloc failed\n");
return FALSE;
}
}
cInv->pList[cInv->curSize] = (Car *)malloc(sizeof(Car));
if(cInv->pList[cInv->curSize] == NULL)
{
printf("malloc failed\n");
return FALSE;
}
cInv->pList[cInv->curSize] = aCar;
printf(" id is cInv->pList[%d]->%d", cInv->curSize, cInv->pList[cInv->curSize]->id);
printf(" make is cInv->pList[%d]->%s", cInv->curSize, cInv->pList[cInv->curSize]->make);
(cInv->curSize)++;
return TRUE;
}
printing in another function
Code:
for(i = 0; i < cInv->curSize; i++)
{
printf("\n%2d %16s %16s %16s %6d %8ld %6s",
cInv->pList[i]->id,
cInv->pList[i]->make,
cInv->pList[i]->model,
cInv->pList[i]->color,
cInv->pList[i]->year,
cInv->pList[i]->price,
carType[cInv->pList[i]->type]
);
}