Structs in C

Structs in C

C arrays allow you to define variables that can store data items of the same type, and structures are another user-defined data type available in C programming that allows you to store data items of different types.

The data members in the structure can be basic data types (such as int, float, char, etc.), or other structure types, pointer types, etc.

A structure is used to represent a record. Suppose you want to track the activity of books in the library. You may need to track the following properties of each book:

  • Title
  • Author
  • Subject
  • Book ID

define structure

A structure definition consists of the keyword struct and a structure name, and the structure name can be defined as required.

The struct statement defines a new data type that contains multiple members. The format of the struct statement is as follows:

struct tag { 
    member-list
    member-list 
    member-list  
    ...
} variable-list ;

tag is a structure tag.

member-list is a standard variable definition, such as int i; or float f;, or other valid variable definitions.

variable-list structure variable, defined at the end of the structure, before the last semicolon, you can specify one or more structure variables. Here is how the Book structure is declared:

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;

In the above declaration, the first and second declarations are regarded as two completely different types by the compiler, even if their member lists are the same, if t3=&s1, it is illegal.

Members of a structure can contain other structures, and can also contain pointers to their own structure types, and usually the application of this pointer is to implement some more advanced data structures such as linked lists and trees.

Initialization of structure variables

Like other types of variables, the initial value can be specified when defining a structure variable.

access structure members

To access members of a structure, we use the member access operator (.). The member access operator is a period between the structure variable name and the structure member we want to access. You can use the struct keyword to define variables of structure type.

Let's take an example to see the definition and use of the structure. We know that the point on the plane is determined by its coordinates on the x-axis and the coordinates on the y-axis, so if we want to describe a point on the plane in the program, we need to define two attribute variables x of this point and y, for example:

insert image description here

float point0_x = 1;
float point0_y = 3;
float point1_x = 2;
float point1_y = 4;

For such coordinate points, x and y in a point-related variable always appear in pairs, that is to say: for the coordinates of points in the plane, there must be two attributes, x and y. So we can define these two attributes (two variables) as a structure, and the name is called the coordinate variable of the point in the plane, referred to as point:

struct point
{
    float x;
    float y;
};

In this way, we have a variable type called "point type", which means: as long as this variable type is defined, the variable of this type will have two attributes (x and y). Next we can use this type to define two points p0 and p1,

struct point p0;
struct point p1;
p0.x = 1;
p0.y = 2;
p1.x = 3;
p1.y = 1;

structure nesting

Above we defined the coordinates of two points on the plane, and then we have the coordinates of two points on the plane. With the points, we can define a new variable type called rectangle according to the structure of the points. : struct rect.

insert image description here
Let's define a rectangular structure, in which the point structure is nested, and the use is consistent with the normal structure. Let's take a look at this example first, and write a function to calculate the area of ​​a rectangle:

#include <stdio.h>
#include <math.h>
struct point
{
    float x;
    float y;
};
struct rect
{
    struct point p0;
    struct point p1;
};
float area_of_rect(struct rect r)
{
    return fabs(r.p1.x - r.p0.x) * fabs(r.p1.y - r.p0.y);
}
int main(int argc, char *argv[])
{
    struct rect r;
    r.p0.x = 1;
    r.p0.y = 2;
    r.p1.x = 3;
    r.p1.y = 1;
    printf("%f\n", area_of_rect(r));
    return 0;
}

simplified definition

Of course, here we define the structure variable and use it as an input parameter of a function. We will learn about structures as function parameters and function return values ​​in the next section. In addition, when we define a structure, we can also define variables of this structure type directly after the definition:

typedef struct student
{
    int id;
    int age;
    char name[10];
} zhangs,lis,wangw;

In this way, a structure type called student is defined, and then three variables of the student type, zhangs, lis, and wangw, are directly defined.

In addition, when we use structure variables, we always have to use the form of struct name var, which is more or less troublesome. In order to make the program more concise, we can use the typedef keyword to redefine the structure type. For example:

typedef struct student
{
    int id;
    int age;
    char name[10];
} stu;
stu s0;
stu s1;
stu s2;

In this way, a structure called struct student is defined, and it is redefined as stu type through typedef, and then we can define this structure type variable through stu. Using stu in the program means struct stuent, which is much simpler, but please note: if the name after the brace structure in the typedef structure definition is used, it is the new name of the structure, and if the typedef structure definition is not used The text after the end of the curly braces in means that the variables of this structure are defined.

Guess you like

Origin blog.csdn.net/QYgujingjing/article/details/129920891