Programming C: Posts 13 – Type of structure – struct

The easiest way to access the type of structure is an example of students. A class 100 students each student include your name and student code. Enter data for the class. Hehe. You think about how to use 2 Plate: 1 array names, 1 array student code right. Right, That way no wrong ... but let's see the next request ... Thi semester finished, Please enter the scores for each student, each student included 10 subjects (Mathematics, Believe, Chemistry, Physics,...). Giờ bạn thấy sao nào… Dùng 12 Did not ... oh array, hãy dùng type of structure. With just type structure 1 Plate only.

1. Type of structure

For arrays, can save a lot of information has the same data type. But with this type of structure we can store information more different data types.

1.1 VD opening

//code by nguyenvanquan7826
#include <stdio.h>
#include <stdlib.h>

// khai bao struct
struct sinhvien 
{
    char MSV[20]; // ma sinh vien
    char hoten[30]; // ho ten sinh vien
    double toan, tin, anh; // cac diem toan, tin, anh
};

/* Hay thay tat ca fflush(stdin); thanh __fpurge(stdin) khi ban lam tren linux*/

int main() 
{
    /* khai bao 2 bien sv1, sv2 va 1 mang
     * CNPMK10A gom 100 sinh vien
     */
    struct sinhvien sv1, sv2, CNPMK10A[100];

    printf("Nhap du lieu cho sv1:\n");
    printf("MSV: "); fflush(stdin);
    gets(sv1.MSV);
    printf("Ho ten: "); fflush(stdin);
    gets(sv1.hoten);
    printf("Diem toan, tin, anh: "); fflush(stdin);
    scanf("%lf %lf %lf", &sv1.toan, &sv1.tin, &sv1.anh);

    printf("Nhap du lieu cho sv2:\n");
    printf("MSV: "); fflush(stdin);
    gets(sv2.MSV);
    printf("Ho ten: "); fflush(stdin);
    gets(sv2.hoten);
    printf("Diem toan, tin, anh: "); fflush(stdin);
    scanf("%lf %lf %lf", &sv2.toan, &sv2.tin, &sv2.anh);

    printf("\n --------- Thong tin sinh vien -----\n");
    printf("%-20s %-30s %-7s %-7s %-7s\n", "MSV", "Ho ten", "Toan", "Tin", "Anh");
    printf("%-20s %-30s %-7.2lf %-7.2lf %-7.2lf\n", sv1.MSV, sv1.hoten, sv1.toan, sv1.tin, sv1.anh);
    printf("%-20s %-30s %-7.2lf %-7.2lf %-7.2lf\n", sv2.MSV, sv2.hoten, sv2.toan, sv2.tin, sv2.anh);

    return 0;
}

Result:

Nhap du lieu cho sv1:
MSV: DTC1
Ho ten: Pham Thi Ha
Diem toan, tin, anh: 9 9 8
Nhap du lieu cho sv2:
MSV: DTC2
Ho ten: Nguyen Van Quan
Diem toan, tin, anh: 9 9 8

 --------- Thong tin sinh vien -----
MSV                  Ho ten                         Toan    Tin     Anh
DTC1                 Pham Thi Ha                    9.00    9.00    8.00
DTC2                 Nguyen Van Quan                9.00    9.00    8.00

At the beginning of this example, chúng ta có rất nhiều điểu phải bàn 🙂

1.2 Construction type structure, declare the variable structure

As the example above, to build 1 type of structure we follow the syntax:

struct TenKieuCauTruc
{
    Khai báo các thành phần của kiểu;
};

After then the type of structure that is very similar style 1 normal type (int, float, tank,...) and we just declare another variable is finished. However, the variable declaration should be added in the previous struct keyword: (For C does not need).

struct TenKieuCauTruc TenBienCauTruc;

In addition we're also building some kind of structure and variable declaration is structured as follows:

struct TenKieuCauTruc
{
    Khai báo các thành phần của kiểu;
} danh sách các biến thuộc kiểu cấu trúc;

Or

struct
{
    Khai báo các thành phần của kiểu;
} danh sách các biến thuộc kiểu cấu trúc ;

With this declaration, It is mandatory to declare variables in the structure as soon as there is no structure type name to our structure declared in another location.
In this section, we should mention 1 important keyword, That is typedef. This keyword is used to define 1 new data types.

typedef struct sinhvien 
{
    char MSV[20]; // ma sinh vien
    char hoten[30]; // ho ten sinh vien
    double toan, tin, anh; // cac diem toan, tin, anh
} kieuSinhVien;

When we have this kieuSinhVien was 1 datatypes (as int, double, ...) and we can declare variables through its structure. In this there are a few things you need to pay attention:

  • With sinhvien (type of structure is placed after the keyword struct) when declaring variables of this type struct we still have in front of it. (CEO: sinhvien SVA; -> Sai còn struct sinhvien svA; -> đúng), (attention in C is not required).
  • With kieuSinhVIen when declaring variables of this type we not have the struct in front of it. (CEO: struct kieuSinhVIen all; -> sai, kieuSinhVIen all; -> đúng).

In addition we can also type declarations nested structures: Eg as in 1 students Birthday, date of birth in the day, month, Year of Birth.

struct ngaysinh 
{
    int ngay, thang, nam;
}

typedef struct sinhvien 
{
    char MSV[20]; // ma sinh vien
    char hoten[30]; // ho ten sinh vien
    double toan, tin, anh; // cac diem toan, tin, anh
    struct ngaysinh ns;
} kieuSinhVien;

Or we declared within the structure:

typedef struct sinhvien 
{
    char MSV[20]; // ma sinh vien
    char hoten[30]; // ho ten sinh vien
    double toan, tin, anh; // cac diem toan, tin, anh
    struct ngaysinh 
    {
        int ngay, thang, nam;
    } ns;
} kieuSinhVien;

1.3 Access to components of the structure

For access to the components of the structure we use the dot (.).

TenBienCauTruc.TenThanhPhan;

As one example on the following access:
sv1.hoten; sv1.toan; // access to their name, Math scores
sv1.ns.ngay; sv1.ns.thang; // access to birth date and month of birth.
...

Once access is to the components of the structure, each component is 1 It is normal variables and assign values ​​or enter the value to them as normal, but we still do.

In addition, if certain elements lengthy, we can avoid the long lines using keywords define.
For example instead of writing:

sv1.ns.thang;
sv1.ns.nam;

We write:

#define p sv1.ns
p.thang;
p.nam;

1.4 Assign the variables have the same type of structure

//code by nguyenvanquan7826
#include <stdio.h>
#include <stdlib.h>

struct sinhvien 
{
    char MSV[20]; // ma sinh vien
    char hoten[30]; // ho ten sinh vien
    double toan, tin, anh; // cac diem toan, tin, anh
};

/* Hay thay tat ca fflush(stdin); thanh __fpurge(stdin) khi ban lam tren linux*/

int main() 
{
    /* khai bao 2 bien sv1, sv2 va 1 mang
     * CNPMK10A gom 100 sinh vien
     */
    struct sinhvien sv1, sv2, CNPMK10A[100];

    printf("Nhap du lieu cho sv1:\n");
    printf("MSV: "); fflush(stdin);
    gets(sv1.MSV);
    printf("Ho ten: "); fflush(stdin);
    gets(sv1.hoten);
    printf("Diem toan, tin, anh: "); fflush(stdin);
    scanf("%lf %lf %lf", &sv1.toan, &sv1.tin, &sv1.anh);

    sv2 = sv1; // gan gia tri cua sv1 cho sv2

    printf("\n --------- Thong tin sinh vien -----\n");
    printf("%-20s %-30s %-7s %-7s %-7s\n", "MSV", "Ho ten", "Toan", "Tin", "Anh");
    printf("%-20s %-30s %-7.2lf %-7.2lf %-7.2lf\n", sv2.MSV, sv2.hoten, sv2.toan, sv2.tin, sv2.anh);

    return 0;
}

After assigning SV2 = SV1, all information of the SV2 SV1 has also. In addition it is also possible to assign initial values ​​for structure.

struct sinhvien sv1 = {"ABC", "Nguyen Van Quan", 9, 9, 8, {4, 5, 1992}};

Then we have the original data is SV1:

MSV: ABC
hoten: Nguyen Van Quan
toan: 9
tin: 9
anh: 8
ngày sinh: 4/5/1992.

2. The array structure

Above we have to learn the basics of structure type and a few examples of structures sinhvien. Now we learn how to perform 1 Plate 50 students 1 class structure type as above. Consider the example:

//code by nguyenvanquan7826
#include <stdio.h>
#include <stdlib.h>

struct sinhvien 
{
    char MSV[20]; // ma sinh vien
    char hoten[30]; // ho ten sinh vien
    double diemTB; // diem trung binh
    struct ngaysinh 
    {
        int ngay, thang, nam;
    } ns;
};

int main() 
{
    int n = 2, i;
    struct sinhvien CNPMK10A[n];

    for (i = 0; i < n; i++) 
    {
        #define sv CNPMK10A[i]
        printf("Nhap du lieu cho sinh vien thu %d:\n", i + 1);
        printf("MSV: "); fflush(stdin)
        gets(sv.MSV);
        printf("Ho ten: "); fflush(stdin);
        gets(sv.hoten);
        printf("Diem TB: "); fflush(stdin)
        scanf("%lf", &sv.diemTB);
        printf("Ngay sinh: ");
        scanf("%d/%d/%d", &sv.ns.ngay, &sv.ns.thang, &sv.ns.nam);
    }   

    printf("\n --------- Thong tin sinh vien -----\n");
    printf("%-20s %-30s %-7s %-10s\n", "MSV", "Ho ten", "Diem Tb", "Ngay sinh");
    for (i = 0; i < n; i++) 
    {
        #define sv CNPMK10A[i]
        printf("%-20s %-30s %-7.2lf %02d/%02d/%4d\n", sv.MSV, sv.hoten, sv.diemTB, sv.ns.ngay, sv.ns.thang, sv.ns.nam);
    }
    return 0;
}

Result:

Nhap du lieu cho sinh vien thu 1:
MSV: DTC1
Ho ten: Pham Thi Ha
Diem TB: 9.2
Ngay sinh: 21/01/1993
Nhap du lieu cho sinh vien thu 2:
MSV: DTC2
Ho ten: Nguyen Van Quan
Diem TB: 9.2
Ngay sinh: 31/12/1992

 --------- Thong tin sinh vien -----
MSV                  Ho ten                         Diem Tb Ngay sinh
DTC1                 Pham Thi Ha                    9.20    21/01/1993
DTC2                 Nguyen Van Quan                9.20    31/12/1992

3. Structure pointer

//code by nguyenvanquan7826
#include <stdio.h>
#include <stdlib.h>

struct sinhvien 
{
    char MSV[20]; // ma sinh vien
    char hoten[30]; // ho ten sinh vien
    double diemTB; // diem trung binh
    struct ngaysinh 
    {
        int ngay, thang, nam;
    } ns;
};

int main() 
{
    int n = 2, i;
    // cap phat bo nho
    struct sinhvien *CNPMK10A = (struct sinhvien*) malloc(n * sizeof(struct sinhvien));

    for (i = 0; i < n; i++) 
    {
        printf("Nhap du lieu cho sinh vien thu %d:\n", i + 1);
        printf("MSV: "); fflush(stdin);
        gets(CNPMK10A[i].MSV);
        printf("Ho ten: "); fflush(stdin);
        gets(CNPMK10A[i].hoten);
        printf("Diem TB: "); fflush(stdin);
        scanf("%lf", &(CNPMK10A+i)->diemTB);
        printf("Ngay sinh: ");
        scanf("%d/%d/%d", &(CNPMK10A+i)->ns.ngay, &(CNPMK10A+i)->ns.thang, &(CNPMK10A+i)->ns.nam);
    }   

    printf("\n --------- Thong tin sinh vien -----\n");
    printf("%-20s %-30s %-7s %-10s\n", "MSV", "Ho ten", "Diem Tb", "Ngay sinh");
    for (i = 0; i < n; i++) 
    {
        #define ns CNPMK10A[i].ns
        printf("%-20s %-30s %-7.2lf %02d/%02d/%4d\n", CNPMK10A[i].MSV, (*(CNPMK10A+i)).hoten, (CNPMK10A+i)->diemTB, ns.ngay, ns.thang, ns.nam);
    }
    return 0;
}

Access the structural components

In order to get the data access components pointer structures have 3 following:

  • How to 1: CNPMK10A[in].diemTB;
  • How to 2: (*(CNPMK10A i)).diemTB;
  • How to 3: (CNPMK10A i) ->diemTB;

Both 3 methods will have access to DTB.

To get the address we have 2 from:

  • How to 1: &CNPMK10A[in].DTB;
  • How to 2: &(CNPMK10A i)->diemTB