Structure

Posted by blueskyson on February 17, 2020

Structure Variables

Suppose you want to store data of a student, you may store his name, age, height, score… Though an array can store several elements, every element must be the same type! For instance, an array cannot store both int and float. So, when we need to store a collection of related data items, it is recommended to use a structure.

Here’s an example of a structure that stores data of student.

1
2
3
4
5
6
struct student {  //structure declaration
    string name;
    int age;
    double height;
    int ID;
};

struct is a reserved key word that tells compiler to make a user-defined structure. student is called a structure name or a structure tag. Every type declared in a structure is called member, so the structure student has 4 members.

structure

The members of a structure are stored in memory in the order in which they’re declared. Now, we can use student as a template to create variables. Use . operator to access members in structure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;

struct student {  //structure declaration
    string name;
    int age;
    double height;
    int ID;
};

int main() {
    student a;  //declare variable a as structure student

    a.name = "Alice";     //use . to access members
    a.age = 13;           //
    a.height = 155.6666;  //
    a.ID = 1234;          //

    cout << "student name  : " << a.name << endl;
    cout << "student age   : " << a.age << endl;
    cout << "student height: " << a.height << endl;
    cout << "student ID    : " << a.ID << endl;

    return 0;
}
student name  : Alice
student age   : 13
student height: 155.667
student ID    : 1234

If you are familiar with structure in C, you may notice that C++ allows you to omit key word struct before structure name. This avoid some error causing by omitting key word struct.

Structure initializers follow rules similar to those for array initializers. Structures also allows us to copy values from one to another.

1
2
3
4
5
6
7
    student b = {"Bob", 14, 175.5, 1233};
    student c = b;  //copy member values from b to c

    cout << "student name  : " << c.name << endl;
    cout << "student age   : " << c.age << endl;
    cout << "student height: " << c.height << endl;
    cout << "student ID    : " << c.ID << endl;
student name  : Bob
student age   : 14
student height: 175.5
student ID    : 1233

We can declare variables right after defining a structure

1
2
3
4
5
struct zoo {
    int lions;
    int penguins;
    int koalas;
} taipei_zoo, helsinki_zoo; //two zoo variables

We can even declare a structure without tag. However, we can’t declare variables of this type any more.

1
2
3
struct {
    int x, y, z;
} position; //a variable that has no tag

Construct and access members of a structure array.

1
2
3
4
5
6
student stu[2] = { {"Alice", 13, 155, 1234},  
                   {"Bob", 14, 175, 1233}
                 };
student class_A[30];
cin >> class_A[0].name;
cout << class_A[0].name;

Structure and sizeof()

1
2
3
4
student arr[30];
cout << "size of struct student: " << sizeof(student) << end;
cout << "size of arr: " << sizeof(arr) << endl;
cout << "size of one element in arr: " << sizeof(arr[0]) << endl;
size of struct student: 48
size of arr: 1440
size of one element in arr: 48

Bit Field

You can assign specific bits to members, this technique are usually implemented to low-level program design. For more information, check out geeksforgeeks : )

1
2
3
4
5
6
struct date {
    unsigned int dd : 5;   //5 bits for day   (1 ~ 31)
    unsigned int mm : 4;   //4 bits for month (1 ~ 12)
    unsigned int yy : 20;  //other bits for year (0 ~ 2^20 - 1)
    unsigned int : 3;      //3 bits unused
};