Mangal Pandey Mangal Pandey - 3 months ago 20
C Question

%d expecting int* in c

As per what I know, %d expects an int value. I don't understand why gcc compiler is saying that it expects int* instead.

Here is my code:

#include<stdio.h>
#include<stdlib.h>

struct student
{
int rn, m[5];
char nm[25];
};

int main()
{
struct student* s[5];
struct student **q=s;

int i = 0;

printf("Enter data");

for(i=0;i<5;i++)
{
printf("\nStudent %d:\n",i+1);
printf("Roll number: ");
scanf("%d",(s[i])->rn);
printf("\n%d",s[i]->rn);
}
}


And here is the warning:

warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%d",(s[i])->rn);

Answer

scanf reads a value into an int, it takes an pointer to the variable to read into.

scanf("%d",&((s[i])->rn));
Comments