1

Why is in the first fscanf function below, the argument of %s has no & but the argument of %d has &?

fscanf(fileR, "%s%d", meds[count].name, &meds[count].unitsInStock);
==>  meds[count].name, &meds[count].unitsInStock
fscanf(fileR, "%i", &meds[count].sixmth[i]);
==>  &meds[count].sixmth[i]
Si Dang
  • 49
  • 1
  • 7

1 Answers1

0

it is because the string is already a pointer, I will explain it on this example:

int * buff;
int n;
buff = &n;

//&n - address of n
//buff - address of n
//*buff - value of n

fscanf(stdin, "%d", &n); //it is only variable, it needs to pass address of it
fscanf(stdin, "%d", buff); //it is pointer to variable, it doesn't need to add &

And with string (char*) it's same:

char* str;
str = malloc(50);
fscanf(stdin, "%s", str); //it is poitner, there is already address
Adam
  • 114
  • 3