I'm trying to create an ADT , first of all, here is the basic information about it:
typedef struct orange_t* Orange;
typedef enum {
JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC,
} Month;
struct orange_t {
short size;
Month expirationMonth;
char** foodCompanies;
int maxNumberOfFoodCompanies;
int sellingPrice;
};
now im trying to create a function that will "create" new orange like that:
Orange orangeCreate(short size,Month expirationMonth
,int maxNumberOfFoodCompanies,int sellingPrice)
{
Orange new_orange=(Orange)malloc(sizeof(struct orange_t));
if(new_orange==NULL)
{
return NULL;
}
if((sellingPrice<0)||(size>256||size<1)||(maxNumberOfFoodCompanies<0)||(expirationMonth>12)
||(expirationMonth<1))
{
return NULL;
}
new_orange->sellingPrice=sellingPrice;
new_orange->maxNumberOfFoodCompanies=maxNumberOfFoodCompanies;
new_orange->expirationMonth=expirationMonth;
new_orange->size=size;
for(int i=0;i<new_orange->maxNumberOfFoodCompanies;i++)
{
new_orange->foodCompanies[i]=NULL;
}
return new_orange;
}
when I tried to check the function with the simple main()
:
int main()
{
Orange orange=orangeCreate(3,JAN,10,4);
printf("the size is %d\n",orange->size);
orangeDestroy(orange);
return 0;
}
the program keeps crashing, I guess I didn't change the values in orange as I should, and they might still be NULL
.
where did I go wrong here?