Question: I'm new to C, and for my class, we have a project where we have to write a function that reads all the items in the file and returns them in file-order, in a dynamically created list.
Each line of the file is one item and has the format:

```
'<description>' <damage> <cost> <weight>

```

It's supposed to return a base pointer to items in file in file order, or NULL if file doesn't exist.

Here's my code so far:

```
item_t *ReadItemsFromFile(char *file)
{
typedef struct item item_t;
struct item
{
   char name[32];
   float cost, weight;
   int dam;
};FILE *fpin = fopen(file, "r");

if(fpin != NULL)
{
  item_t i[20];
  int n = 0;
  char line[sizeof(file)];
  while(fgets(line, sizeof(line), fpin) != NULL){
      (fscanf(fpin, " '%[^']' %d %f %f", i[n].name, &i[n].dam, &i[n].cost, &i[n].weight));
  fputs(i[n].name, stdout);
  n++;
}
} else {
  return NULL;
} 
  return(0);
}

```

I've been using fputs to try to test the code, but it keeps giving me gibberish for the first and last items.
Also, when I try putting

```
fputs(&i[n].dam, stdout);

```

so I can test the other variables for the struct, I keep getting an error saying "passing argument 1 of 'fputs' from incompatible pointer type.

I'm not sure if I'm accurately passing into struct variables using fscanf, or if it's something else.

Answer: 
You need to move the `struct` definition to be outside of your function.

Your specification for how long the line should be is wrong. You need to specify a number of bytes that is long enough to take in an entire line.

When you use `fgets()` to get a line, you should not use `fscanf()` to read from the file. You are already using `fgets()` to read from the file. You use `sscanf()` to read data from the string that is contained within `line`.

The above is enough to get your program to do something nominally useful.

To return a list of items, you will need to actually call `malloc()` (or some other similar function) to create the memory for your "dynamically created list" that you have specified as a requirement. Since you don't know how many items you have up front, you either need a way to discover how many there are, or use a mechanism that allows your list to grow dynamically.
Okay, I stopped using fgets() and moved the struct outside of the function, but I'm still having issues returning the pointer. I keep trying to return the pointer after mallocing it, but it informs me of "return from incompatible pointer type". Here's my code so far:

```
struct items_t
{
   char name[32];
   float cost, weight;
   int dam;
};
typedef struct items_t items_t;

item_t *ReadItemsFromFile(char *file)
{

  FILE *fpin = fopen(file, "r");

if(fpin)
{   
  items_t i[50];
  int n = 0;

   while(4 == (fscanf(fpin, " '%[^']' %d %f %f", i[n].name, &i[n].dam, &i[n].cost, &i[n].weight)))
{
  n++;
  items_t *a = (items_t*)malloc(sizeof(items_t));
  return(&a);

}
} else {
  return NULL; 
}
return(0);

}

```