0

So a friend of mine sent me this but unfortunately my C skills are a bit rusty so I might be missing something easy. The problem exists in the second fscanf command.

The error is [Error] expected primary-expression before '[' token. It might be something it's easily missed but I tried searching for a bit before posting this. Any help is appreciated!

struct materials
{
  char name,product;
  int code,code1,number_of_parts,quantity1,number_of_parts1,quantity2;     
};
typedef struct materials mater;
int main()
{
 int result(int x)  ; 
 int number1,j,i,result1,number2,k,z; 
 FILE *fp;
    materials* listOfMaterials; 
 fp = fopen("datain.txt", "r");

 if (fp!=NULL)
 {
 fscanf(fp, "%d\n",&number1);

 listOfMaterials = (mater*)malloc(number1 * sizeof( mater));

 for(j=1; j<number1; j++) {
  fscanf (fp, "%d %d %d %s\n", &mater[j].code, &mater[j].quantity1, &mater[j].number_of_parts, mater[j].name);
        if (mater[j].quantity1 != 0)
    {
           mater[j].code1=(int *)malloc(sizeof(int)*mater[j].quantity1);
           mater[j].number_of_parts1=(int *)malloc(sizeof(int) * mater[j].quantity1);                     
       fscanf (fp,"%d %d\n",&mater[j].code1,&mater[j].number_of_parts1);      
    }
}
jww
  • 97,681
  • 90
  • 411
  • 885
ahcomm
  • 1
  • 1
    Please sort out the indentation of the code - makes it readable. – Ed Heal Dec 07 '14 at 09:19
  • Possible duplicate of [How to use scanf \ fscanf to read a line and parse into variables?](http://stackoverflow.com/questions/5204625/how-to-use-scanf-fscanf-to-read-a-line-and-parse-into-variables) – jww Dec 07 '14 at 09:23
  • Sorry I just signed up here. What can I do to make it more readable? – ahcomm Dec 07 '14 at 09:25
  • `mater[j]` --> `listOfMaterials[j]` , also `j` start from `1`, not `0` ?? – BLUEPIXY Dec 07 '14 at 09:30
  • Also, don't cast the return of `malloc`, https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc, best is to use no casts at all in C. – Jens Gustedt Dec 07 '14 at 09:31

2 Answers2

2

typedef struct materials mater defines a new type named mater.

Further in the code you attempt to use it as a variable, so I guess you meant define a variable, rather than a type:

struct materials mater;

or just:

materials mater;
Igor R.
  • 14,716
  • 2
  • 49
  • 83
  • I haven't touched C in some months. Mater is my struct variable but the thing I don't understand is if I can consider it as a string so I can do `&mater[j].x`. – ahcomm Dec 07 '14 at 09:55
  • @ahcomm No, `name` and `product` sturct members are single `char`s, not strings. If you want them to be strings, define them as `char *name, *product;`, then *allocate* each of them with `malloc(n)`, so that they will have enough room for `n` chars (incuding null terminator). Or just use C++, and avoid all this mess :). – Igor R. Dec 07 '14 at 10:42
  • I see. As I said a friend of mine asked me to help him with this and I don't want to provide him with false information. Thanks. – ahcomm Dec 07 '14 at 11:02
  • @ahcomm use plain C only you must (kernel module, class assignment, etc.), otherwise use C++ with its Standard Library. – Igor R. Dec 07 '14 at 11:18
1

mater is a type and not an object, your scanf line makes not much sense to me.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177