Structure types are not considered a variable declaration, just definition of a new type, so they cannot store anything until we declare variable of this type.
Here is how we would create:
type_name_of_struct name_of_variable;
Creating three variables a, b, c of the Student type:
Creating an array of the Student type:
A member of a structure may have any desired complete type, including previously defined structure types. They must not be variable-length arrays, or pointers to such arrays. For instance, now we want to record more information of students, for example their date of birth, which comprises the day, month and year. So first, let's start with the date, because that is a new type that we may be able to use in a variety of situations. We can declare a new type for a Date thus:
struct Date
{
int day;
int month;
int year;
};
We can now use this Date type, together with other types, as members of a Student type which we can declare as follows:
struct Student
{
char studentID[10];
char name[30];
float markCS ;
Date dateOfBirth;
};
Or
struct Student
{
char studentID[10];
char name[30];
float markCS;
struct Date {
int day;
int month;
int year;
} dateOfBirth;
};
We can also declare structured variables when we define the structure itself:
struct Student
{
char studentID[10];
char name[30];
float markCS ;
Date dateOfBirth;
} a, b, c;
C permits to declare untagged structures that enable us to declare structure variables without defining a name for their structures.
For example, the following structure definition declares the variables a, b, c but omits the name of the structure:
struct
{
char studentID[10];
char name[30];
float markCS ;
Date dateOfBirth;
} a, b, c;
A structure type cannot contain itself as a member, as its definition is not complete until the closing brace (}). However, structure types can and often do contain pointers to their own type. Such self-referential structures are used in implementing linked lists and binary trees, for example. The following example defines a type for the members of a singly linked list:
struct List
{ struct Student stu; // This record's data.
struct List *pNext; // A pointer to the next student.
};