Home > Net >  Incompatible pointer type file txt for loop
Incompatible pointer type file txt for loop

Time:10-10

I am getting an error for the below code. I was wondering if I am missing memory allocation or what could be the pointer issue. Any help would be wonderful , thank you

#include <stdio.h>
#include <string.h>
#define MAX_ITEM_NAME_STRING    25
#define MAX_ITEM_DESCRIPTION_STRING     80

typedef struct 
{
    char name[MAX_ITEM_NAME_STRING]; //25 value
    char description[MAX_ITEM_DESCRIPTION_STRING]; //80 value
}Item;

int ItemReadItems(Item *items[], int maxItems, char *filename){
    FILE * itemFile = fopen(filename, "r");
    // file is 6 lines "comb","a small comb"[![enter image description here][1]][1]
    char ch;// loads a list of items from a txt file with comma delimited fields
    char line[MAX_ITEM_DESCRIPTION_STRING   MAX_ITEM_NAME_STRING   10];
    int i =0;
    if(itemFile == NULL)
    { return -1;}// if not able reutrn -1 unable to open
    for(i<maxItems; i  )
    {// fills item array items with contents of a file from items.txt
        
        while(fgets(line, sizeof(line), itemFile) != NULL){
          strcpy(items[i]->name, strtok(line, ","));
          strcpy(items[i]->description, strtok(line, ","));
        }
    }
    fclose(itemFile);
    return i;// return a # of objects loaded from filename   
}

Error:

/mnt/c/Users/liloc/Documents/CSCV352/Exercise 6 Files/Exercise6_1/Item.c: In function ‘ItemReadItems’:
/mnt/c/Users/liloc/Documents/CSCV352/Exercise 6 Files/Exercise6_1/Item.c:15:15: warning: passing argument 1 of ‘fgets’ from incompatible pointer type [-Wincompatible-pointer-types]
   15 |         fgets(line, sizeof(line), itemFile);
      |               ^~~~
      |               |
      |               char **
In file included from /mnt/c/Users/liloc/Documents/CSCV352/Exercise 6 Files/Exercise6_1/Item.h:3,
                 from /mnt/c/Users/liloc/Documents/CSCV352/Exercise 6 Files/Exercise6_1/Item.c:1:
/usr/include/stdio.h:564:14: note: expected ‘char * restrict’ but argument is of type ‘char **’
  564 | extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
      |              ^~~~~
/mnt/c/Users/liloc/Documents/CSCV352/Exercise 6 Files/Exercise6_1/Item.c:16:39: warning: passing argument 1 of ‘strtok’ from incompatible pointer type [-Wincompatible-pointer-types]
   16 |         strcpy(items[i]->name, strtok(line, ","));
      |                                       ^~~~
      |                                       |
      |                                       char **
In file included from /mnt/c/Users/liloc/Documents/CSCV352/Exercise 6 Files/Exercise6_1/Item.h:4,
                 from /mnt/c/Users/liloc/Documents/CSCV352/Exercise 6 Files/Exercise6_1/Item.c:1:
/usr/include/string.h:336:14: note: expected ‘char * restrict’ but argument is of type ‘char **’
  336 | extern char *strtok (char *__restrict __s, const char *__restrict __delim)
      |              ^~~~~~
/mnt/c/Users/liloc/Documents/CSCV352/Exercise 6 Files/Exercise6_1/Item.c:17:46: warning: passing argument 1 of ‘strtok’ from incompatible pointer type [-Wincompatible-pointer-types]
   17 |         strcpy(items[i]->description, strtok(line, ","));
      |                                              ^~~~
      |                                              |
      |                                              char **
In file included from /mnt/c/Users/liloc/Documents/CSCV352/Exercise 6 Files/Exercise6_1/Item.h:4,
                 from /mnt/c/Users/liloc/Documents/CSCV352/Exercise 6 Files/Exercise6_1/Item.c:1:
/usr/include/string.h:336:14: note: expected ‘char * restrict’ but argument is of type ‘char **’
  336 | extern char *strtok (char *__restrict __s, const char *__restrict __delim)
      |              ^~~~~~

CodePudding user response:

As @WhozCaig said change:

char *line[MAX_ITEM_DESCRIPTION_STRING   MAX_ITEM_NAME_STRING   10];

to:

char line[MAX_ITEM_DESCRIPTION_STRING   MAX_ITEM_NAME_STRING   10];

What is the 10? Define another constant for that.

ch is not used. Remove it.

fgets() returns NULL on error or EOF. Use that instead of if(feof() {...}. You don't want to execute those strcpy() when fgets() fails.

  • Related