0
votes

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);
2
Change this struct students{ char name[30]; int points[10]; int absences[10]; }; to struct students{ char name[30]; int points; int absences; }; And this scanf("%s", &stud[i].name); --> scanf("%s", stud[i].name); since name itself an address hence & is not required.Achal
Instead of having magical numbers like 30 and 10 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
@Achal Thank you very much :)Stan Marsh

2 Answers

1
votes
  1. In struct students, int points[10]; should be int points;, int absences[10]; should be int absences;

  2. The line scanf("%s", &stud[i].name); should be scanf("%s", stud[i].name);

The follow code could work:

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

struct students{
    char name[30];
    int points;
    int absences;
};

int main()
{
    int i, n;
    printf("Declare the number of students: ");
    scanf("%d", &n);

    struct students *stud = malloc(sizeof(struct students) * 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);

    }
    return 0;
}
-1
votes

Try this code

#include <stdio.h>
struct students{
    char name[30];
    int points;
    int absences;
};

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);

    }
    return 0;
}