C语言 - fscanf函数

时间:2017-11-09 21:28:30

标签: c scanf

为什么在下面的第一个fscanf函数中,%s的参数没有&但是%d的论点有&?

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]

1 个答案:

答案 0 :(得分:0)

这是因为字符串已经是指针,我将在这个例子中解释它:

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 &

使用字符串(char *)它是相同的:

char* str;
str = malloc(50);
fscanf(stdin, "%s", str); //it is poitner, there is already address
相关问题