I want to fill up these arrays with a string and some numbers, but can't really seem to figure out why I cannot.
#include <stdio.h>
struct students{
char name[30];
int points[10];
int absences[10];
};
int main()
{
int i, n;
printf("Declare the number of students: ");
scanf("%d", &n);
struct students stud[n];
for (i = 0; i < n; i++) {
printf("Name: ");
scanf("%s", &stud[i].name);
printf("Points: ");
scanf("%d", &stud[i].points);
printf("Absences: ");
scanf("%d", &stud[i].absences);
}
for( i = 0; i < n; i++)
{
printf("%s\n", stud[i].name);
printf("%d\n", stud[i].points);
printf("%d\n", stud[i].absences);
}
}
This is the warning i get:
warning: format '%s' expects argument of type 'char ', but argument 2 has type 'char ()[30]' [-Wformat=]
scanf("%s", &stud[i].name);
feladat1.c:21:15: warning: format '%d' expects argument of type 'int ', but argument 2 has type 'int ()[10]' [-Wformat=]
scanf("%d", &stud[i].points);
feladat1.c:23:15: warning: format '%d' expects argument of type 'int ', but argument 2 has type 'int ()[10]' [-Wformat=]
scanf("%d", &stud[i].absences);
feladat1.c:30:16: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
printf("%d\n", stud[i].points);
feladat1.c:31:16: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
printf("%d\n", stud[i].absences);
struct students{ char name[30]; int points[10]; int absences[10]; };
tostruct students{ char name[30]; int points; int absences; };
And thisscanf("%s", &stud[i].name);
-->scanf("%s", stud[i].name);
sincename
itself an address hence&
is not required. – Achal30
and10
in your code, define constants that represent them. This helps avoid mistakes when you change several instances to a new value, but forget some others. C is a very unforgiving language, it will do exactly what you ask, so you need to go out of your way to be careful and not set yourself up for problems. – tadman