Initializing an array of structures in C

I am doing a simple exercise for a student database and I am not sure how to initialize an array of structures. I am trying to initialize the first 3 elements of an array stdt[]

with values ​​known at compile time and then the following information from three students will be populated from user input. When I compile, I get an error:

lab7.c: In function β€˜main’:

lab7.c:16:9: error: expected expression before β€˜{’ token
 stdt[0]={"John","Bishop","s1234","Inf",'m',18};
         ^

lab7.c:17:9: error: expected expression before β€˜{’ token
 stdt[1]={"Lady","Cook","s2345","Eng",'f',21};
         ^

lab7.c:18:9: error: expected expression before β€˜{’ token
 stdt[2]={"James","Jackson","s33456","Eng",'m',17};
         ^

      

How can I make it right?

Here is the code:

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

typedef struct {
    char *name;
    char *surname;
    char *UUN;
    char *department;
    char gender;
    int age;
} student_t;

int main() {
    int i;
    student_t stdt[6];
    stdt[0]={"John","Bishop","s1234","Inf",'m',18};
    stdt[1]={"Lady","Cook","s2345","Eng",'f',21};
    stdt[2]={"James","Jackson","s33456","Eng",'m',17};

    for(i=3;i<6;i++) {
        printf("First name: \n");
        scanf("%s",stdt[i].name);
        printf("Last name: \n");
        scanf("%s",stdt[i].surname);
        printf("UUN: \n");
        scanf("%s",stdt[i].UUN);
        printf("Department: \n");
        scanf("%s",stdt[i].department);
        printf("Gender (m/f): \n");
        scanf("%c",stdt[i].gender);
        printf("Age: \n");
        scanf("%d",stdt[i].age);
    }
    return 0;
}   

      

+3


source to share


2 answers


You are not "initialized" if you do not do so at creation time. You can do:

student_t stdt[2] = { {"John", "Bishop", "s1234", "Inf", 'm', 18},
                      {"Lady", "Cook", "s2345", "Eng", 'f', 21}
                    };

      

as much as you have.

It's ok not to explicitly provide a value for every element in the array. For those you don't explicitly initialize, pointer NULL

members will be implicitly initialized to , and numeric members will be implicitly initialized to 0

. In other words, it is:

student_t stdt[4] = { {"John", "Bishop", "s1234", "Inf", 'm', 18},
                      {"Lady", "Cook", "s2345", "Eng", 'f', 21}
                    };

      

is equivalent to this:

student_t stdt[4] = { {"John", "Bishop", "s1234", "Inf", 'm', 18},
                      {"Lady", "Cook", "s2345", "Eng", 'f', 21},
                      {NULL, NULL, NULL, NULL, 0, 0},
                      {NULL, NULL, NULL, NULL, 0, 0}
                    };

      



For the curious, these rules follow from the C standard as follows.

From section C11 6.7.9.21:

If there are fewer initializers in the list enclosed in curly braces than there are elements or elements of a collection, or fewer characters in the string literal used to initialize an array of known size than there are elements in the array, the rest of the collection must be implicitly initialized the same way. as objects having static storage duration.

and for "same as objects with static storage duration" we have section 6.7.9.10:

If an object with automatic storage duration is not explicitly initialized, its value is undefined. If an object that has a static or stream storage duration is not explicitly initialized, then:

  • if it is of pointer type, it is initialized with a null pointer,

  • if it is an arithmetic type, it is initialized (positive or unsigned) to zero;

  • if it is an aggregate, each member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

  • if it is a union, the first named element is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

A struct

represents "collection" in the sense of the above third notation.

+8


source


You can do almost what you wrote if you use C99 "compound literals":

stdt[0] = (student_t){ "John",  "Bishop",  "s1234",  "Inf", 'm', 18 };
stdt[1] = (student_t){ "Lady",  "Cook",    "s2345",  "Eng", 'f', 21 };
stdt[2] = (student_t){ "James", "Jackson", "s33456", "Eng", 'm', 17 };

      



However, this assigns values ​​to the array, rather than initializes the array. In addition, because the array is local to main()

, the values ​​in the other three array elements are undefined; you cannot know anything about them.

+2


source







All Articles