Hi, I'm new to C programming and in my assignments I'm required to create a multidimensional linked list. When I was compiling, one of the nodes' data gave me a problem of:
passing argument 1 and 2 of 'strcpy()' makes pointer from integer without a cast. I was wondering if you all could help me take a look at what's wrong with it?
Uhm this is the header file:
Code:
typedef struct price
{
unsigned dollars;
unsigned cents;
} PriceType;
typedef struct item
{
char itemID[ID_LEN + 1];
char itemName[MAX_NAME_LEN + 1];
PriceType prices[NUM_PRICES];
char itemDescription[MAX_DESC_LEN];
ItemTypePtr nextItem;
} ItemType;
typedef struct category
{
char categoryID[ID_LEN + 1];
char categoryName[MAX_NAME_LEN + 1];
char fareType[MIN_DESC_LEN]; /* (F)ull or (C)oncession */
char categoryDescription[MAX_DESC_LEN];
struct category *nextCategory;
ItemTypePtr headItem;
unsigned numItems;
} CategoryType;
typedef struct fms
{
CategoryTypePtr headCategory;
unsigned numCategories;
} FMSType;
and this is the part in the function that has problems:
Code:
int loadData(FMSType* fms, char* fareFile, char* subfareFile)
{
CategoryTypePtr currCat, prevCat = NULL, newCat;
ItemTypePtr currIt, prevItem = NULL, newItem;
FILE *fp1, *fp2;
char *ptr;
char line[MAX_DESC_LEN];
char temp1[MAX_DESC_LEN];
char temp2[MAX_DESC_LEN];
char temp3[MAX_DESC_LEN];
char temp4[MAX_DESC_LEN];
char check[MAX_DESC_LEN];
char dollars[MAX_DESC_LEN], cents[MAX_DESC_LEN];
int dollarVar, centsVar;
/*code of the first linked list*/
fp2 = fopen(subfareFile, "r");
if(fp2 == NULL){
printf("Unable to open file %s\n", subfareFile);
exit(EXIT_SUCCESS);
}
else {
printf("\n fopen successfully opened the file %s\n\n", subfareFile);
}
while( fgets( line, MAX_DESC_LEN, fp2 ) != NULL){
char* ptr = strtok(line, "|");
strcpy(temp1, ptr);
ptr = strtok(NULL, "|");
strcpy(check, ptr);
ptr = strtok(NULL, "|");
strcpy(temp2, ptr);
ptr = strtok(NULL, "|");
strcpy(temp3, ptr);
ptr = strtok(NULL, "|");
strcpy(temp4, ptr);
char *ptr1 = strtok(temp3, ".");
strcpy(dollars, ptr1);
ptr1 = strtok(NULL, ".");
strcpy(cents, ptr1);
dollarVar = atoi(dollars);
centsVar = atoi(cents);
currCat = fms->headCategory;
while(currCat != NULL){
if(strcmp(currCat->categoryID, check) == 0){
currIt = currCat->headItem;
prevItem = NULL;
while ( currIt != NULL){
prevItem = currIt;
currIt = currIt->nextItem;
}
if((newItem = malloc(sizeof(struct item))) == NULL){
fprintf(stderr, "Could not allocate %d bytes of memory\n", sizeof(CategoryType));
return EXIT_FAILURE;
}
newItem->nextItem = NULL;
strcpy(newItem->itemID, temp1);
strcpy(newItem->itemName, temp2);
strcpy(currIt->prices[0].dollars, dollarVar); /*<----- error here*/
strcpy(currIt->prices[0].cents, centsVar); /*<----- error here*/
strcpy(newItem->itemDescription, temp4);
if(prevItem == NULL){
currCat->headItem = newItem;
}
else{
prevItem->nextItem = newItem;
}
currCat->numItems++;
break;
}
currCat = currCat->nextCategory;
}
}
I don't quite get it how should I do this part where the PriceType prices and it's unsigned dollars and cents. How should I fix this error? Please do advice. Thanks in advance!