Tengo un problema con un puntero que me esta volviendo loco, hace lo que le da la gana, reserve o no memoria para el. Aqui dejo la estructura:
/*****************************************************************************
PRAC3
******************************************************************************/
#ifndef MARNET_H
#define MARNET_H
/*****************************************************************************
Constants
******************************************************************************/
#define MAX_SUPPLIERS 20
#define MAX_CATEGORIES 5
#define MAX_NAME 300
#define MAX_PHONE 9
#define MAX_SIZES 3
/*****************************************************************************
TADs
******************************************************************************/
typedef enum{white, pine, cherryTree, black} tColor;
typedef enum{hall, dinningRoom, bedroom, ofice, kitchen, washroom} tCategoryName;
typedef enum{cPending, inWarehouse, deliveredToClient} tCustState;
typedef enum{sPending, received } tSupState;
typedef char tString[MAX_NAME+1];
typedef enum { FALSE, TRUE } bool;
typedef struct
{
int day, month, year;
int hour, minutes;
}tDate;
typedef struct tFurniture
{
int idFurniture;
int idSup;
tString nameFur;
float sizes[MAX_SIZES];
tString typeFur;
tColor color;
float costPrice;
float retailPrice;
int deliveryTime;
struct tFurniture *prevFur, *nextFur;
struct tFurniture *prevFurType, *nextFurType;
}tFurniture;
typedef struct tType
{
tString typeName;
tFurniture *firstFur, *currFur;
struct tType *prevType, *nextType;
} tType;
typedef struct
{
tCategoryName catName;
tType *primType, *currType;
} tCategory;
typedef struct
{
int idSupplier;
tString name;
tString addr;
int phone[MAX_PHONE];
tFurniture *firstFur, *currFur;
float profitMargin;
}tSupplier;
typedef struct tOrderRow
{
int idProv;
int idFur;
int unit;
float unitCostPrice;
float profitMargin;
struct tOrderRow *nextOrderRow, *nextOrderRowSup;
}tOrderRow;
typedef struct tCustomerOrder
{
int idOrder;
tString nameCust;
tString addrCust;
int phoneCust[MAX_PHONE];
tDate createdDate;
tCustState state;
tOrderRow *firstItem, *currItem;
struct tCustomerOrder *nextCustOrder;
}tCustomerOrder;
typedef struct tSupplierOrder
{
int idOrder;
int idSupplier;
tDate createdDate;
tDate receivedDate;
tSupState state;
tOrderRow *firstItem, *currItem;
float totalPrice;
struct tSupplierOrder *nextSupOrder;
}tSupplierOrder;
typedef struct
{
tSupplier suppliers[MAX_SUPPLIERS];
int currSupplier, numSupplier;
tCategory categories[MAX_CATEGORIES];
tCustomerOrder *firstCustOrder, *currCustOrder;
tSupplierOrder *firstSupOrder, *currSupOrder;
}tShop;
Estas son las llamadas que me estan volviendo loco:
void cat_add_furniture (tCategory *c, tFurniture *f )
{
tType *typeAux, *typeAux2;
tFurniture *furAux;
typeAux
= (tType
*) malloc (sizeof(tType
)); if (typeAux == NULL)
{
printf("ERROR cat_add_furniture: not enough memory\n"); f = NULL;
return;
}
furAux
= (tFurniture
*) malloc (sizeof(tFurniture
)); if (furAux == NULL)
{
printf("ERROR cat_add_furniture: not enough memory\n"); f = NULL;
return;
}
/* Buscamos en la lista tType bajo tCategory*/
typeAux = c->primType;
while( (typeAux->nextType != NULL) && (typeAux->typeName != f->typeFur))
{
typeAux = typeAux->nextType;
}
/* Comprobamos si ya tenemos el tType en la lista, sino hemos de añadir un nuevo tType*/
if( typeAux->typeName == f->typeFur)
{
/* Ya tenemos el tipo en la lista, buscamos la posición donde insertar el nuevo mueble.
También comprobamos que el mueble no haya sido ya insertado.*/
furAux = typeAux->firstFur;
while(((furAux->nextFurType != NULL) && (f->idFurniture!=furAux->idFurniture)) || (f->retailPrice > furAux->retailPrice))
{
furAux = furAux->nextFurType;
}
if ( (furAux->idFurniture != f->idFurniture) || (f->retailPrice > furAux->retailPrice) )
{
/* Tenemos a furAux apuntando a la ultima posición de la lista o al mueble siguiente
ordenado por PVP. Añadimos el nuevo mueble, si no existe ya en la lista */
f->nextFurType = furAux->nextFurType;
f->prevFurType = furAux->prevFurType;
furAux->nextFurType = f;
}
}
else
{
/* No hemos encontrado el tipo y typeAux esta posicionado al final de la lista. Añadimos el nuevo tipo */
typeAux2
= (tType
*) malloc (sizeof(tType
)); if(typeAux2==NULL)
{
printf("ERROR cat_add_furniture: not enough memory\n"); f = NULL;
return;
}
typeAux->nextType = typeAux2;
strcpy(typeAux
->typeName
, f
->typeFur
); typeAux2->prevType = typeAux;
typeAux2->nextType = NULL;
typeAux2->firstFur = f;
typeAux2->currFur = f;
f->nextFurType = NULL;
f->prevFurType = NULL;
}
}
int sup_add_furniture(tShop *t)
{
int i,idSup,idFur,delivery_time,numCat;
float margin,cost,retail_price,s[MAX_SIZES];
bool found;
tString name,type;
tColor color;
margin = 0.0;
/* Leemos los datos del mueble a añadir */
idSup = read_integer ();
idSup = idSup - 1;
idFur = read_integer ();
printf("\nFurniture Name : "); read_string (name);
printf("\nFurniture Type : "); read_string (type);
printf("\nFurniture width : "); s[0]= read_real ();
printf("\nFurniture Height : "); s[1] = read_real ();
printf("\nFurniture Depth : "); s[2] = read_real ();
printf("\nFurniture Colour : "); color = read_colour ();
printf("\nFurniture cost price : "); cost = read_real();
printf("\nFurniture Delivery time : "); delivery_time = read_integer ();
printf("\nHow many categories? "); numCat = read_integer ();
if (numCat>MAX_CATEGORIES)
numCat = MAX_CATEGORIES;
tCategory cat[numCat];
for (i=0;i<numCat;i++)
{
cat[i].catName = hall;
cat[i].primType = NULL;
cat[i].currType = NULL;
}
for (i=0;i<numCat;i++)
{
printf("\nEnter a new Category (H:Hall D:diningroom O:office K:Kitchen B:Bedroom W:Washroom): "); cat[i].catName = read_category();
if (cat[i].catName == -1)
{
error ("sup_add_furniture: undefined category");
return cat[i].catName;
}
}
/* Buscar proveedor al cual tenemos que añadir el mueble. Si no existe acaba el programa con un mensaje de error */
if (search_supplier (*t,idSup) == -1)
{
error (ERROR_SUPPL_NOT_FOUND);
idSup = -1;
return idSup;
}
/* Crear el objeto */
tFurniture
*newFur
= (tFurniture
*) malloc (sizeof(tFurniture
)); if(newFur==NULL)
{
printf("ERROR sup_add_furniture: not enough memory\n"); idSup= -1;
return idSup;
}
*newFur = newFurniture ();
/* Actualizamos campos del objeto */
fur_set_idSupplier( newFur, idSup );
fur_set_id( newFur, idFur );
fur_set_name( newFur, name );
fur_set_color( newFur, color );
fur_set_costPrice( newFur, cost );
fur_set_deliveryTime(newFur, delivery_time );
fur_set_sizes( newFur, s );
fur_set_type( newFur, type );
/* Calcular PVP del mueble : precio de coste + margen de beneficio del proveedor */
if (idSup > -1) margin = sup_get_profitMargin (t->suppliers[idSup]);
margin = 1 + (margin / 100);
retail_price = cost * margin;
fur_set_retailPrice( newFur, retail_price );
/* Añadir el mueble, dentro de la estructura tShop, a las categorias y tipos correspondientes */
for (i=0;i<numCat;i++)
cat_add_furniture (&cat[i], newFur );
if (newFur != NULL)
idSup = newFur->idSup;
else
idSup = -1;
return idSup;
}
En concreto en la funcion cat_add_furniture, a partir de las lineas donde esta el comentario, /* Buscamos en la lista tType bajo tCategory*/, parece como que no asigna el valor de c->primType a typeAux. Y he probado reservandole memoria y sin reservasela. El resultado siempre es el mismo, entra en el bucle del siguiente while aunque sea el primer registro que graba, y no debería ser así.
Alguien puede aportarme algo de luz al caso.
Gracias.
Saludos.
Chema.