C language - structure I

Posted by bruceg on Tue, 08 Feb 2022 20:01:08 +0100

This article is a preliminary study of the structure. It is divided into the following four parts.

1. Declaration of structure type

2. Structure initialization

3. Visits of structural members

4. Structural transmission parameters

1. Declaration of structure type

The objects we want to describe in life are generally complex objects. For example, if I want to describe a person, we need to describe the person's gender, age, height, weight, etc., that is, we can't describe him with a simple value. In c language, if we also want to describe such complex variables, we can use structures.

A structure is a collection of values that become member variables. Each member of a structure can be a variable of different types.

So how to create a structure variable? First, we need to declare the structure type.

struct Book
{
    char name[20];
    int price;
};

In this way, we declare a structure type named struct Book.

When we declare a struct type, how do we use it? Let's look at the code below.

struct Book
{
    char name[20];
    int price;
};
int main()
{
    struct Book b;//We use the structure type struct Book to create a structure variable b
    //b.name="c language"; We can't use this way of assignment between, because name is an array
    strcpy(b.name."c language");//To assign a value to an array, you should use the function strcpy
    b.price=55;
    printf("%s\n",b.name);
    printf("%d\n",b.price);
    return 0;
}

b here is the structure variable we created. We can also create multiple structure variables

struct Book
{
    char name[20];
    int price;
}b4,b5,b6;
int main()
{
    struct Book b1;
    struct Book b2;
    struct Book b3;
    strcpy(b.name."c language");
    b.price=55;
    printf("%s\n",b.name);
    printf("%d\n",b.price);
    return 0;
}

As shown in the above code, I created six structural variables b1~b6. The difference between them is that b1, b2 and b3 are local variables and b4, b5 and b6 are global variables

In addition, we have a way to create structural variables

typedef struct Book
{
    char name[20];
    int price;
}Book;
int main()
{
    Book b1;
    struct Book b2;
    strcpy(b.name."c language");
    b.price=55;
    printf("%s\n",b.name);
    printf("%d\n",b.price);
    return 0;
}

That is to rename the structure type with typrdef. I renamed the structure type to Book. When creating the structure variable, use struct Book or Book.

It is worth noting that when we declare a structure, memory does not allocate space. Only when we create a structure variable, memory will allocate space to its members.

Structure members can be scalars, arrays, pointers, or even other structures.

2. Initialization of structure variables

The initialization of structure variables is similar to that of arrays. Members are initialized one by one with a brace.

struct Book
{
    char name[20];
    int price;
};
int main()
{

    struct Book b={"c language",55};
    return 0;
}

3. Visits of structural members

It mainly uses two operators

.    Structure variable.member name
->   Structure pointer->member name

Let's take a look at the usage of these two kinds of access

struct Book
{
    char name[20];
    int price;
};
void print1(struct Book b)
{
    printf("%s %d\n",b.name,b.price);
}
void print2(struct Book* pb)
{
    printf("%s %d\n",(*pb).name,(*pb).price);
    printf("%s %d\n",pb->name,pb->price);
}
int main()
{
    struct Book s={"c language",55};
    print1(s);
    print2(&s);
    return 0;
}

We created two printing functions to print the members of s with the value and address of structure variable s respectively. Print1 is the value transfer call and print2 is the address transfer call. In fact, the two calling processes are different. In print1, the formal parameter b is actually a temporary copy of S. in printing, it is also a member of b. However, in print2, it is different. The address of S is passed to print2, so the structure variable s itself is printed when printing.

4. Structural transmission parameters

In the above code, we have two ways to pass parameters, so which way is better?

When the structure makes a value transfer call, another copy will be temporarily copied in memory, which will cause a waste of space and increase the system overhead. When we use address calling, we only need to pass the address, which will save space and improve the efficiency of the program.

When transferring parameters to a function, the parameters need to be stacked. If the structure is too large when passing a structure object, the system overhead of parameter stack pressing will be large, which will lead to performance degradation. Therefore, when transferring parameters, we should give priority to address calling.

Topics: C Back-end