Defining Struct Variables
The following example illustrates how to define a struct variable.
struct string { char name[8]; int age; char sex[2]; char depart[20]; float wage1, wage2, wage3, wage4, wage5; }person;
This example defines a struct variable named person of type string. The variable name person can also be omitted, defined as follows:
struct string { char name[8]; int age; char sex[2]; char depart[20]; float wage1, wage2, wage3, wage4, wage5; }; struct string person; // Define a struct variable named person of type string
To define multiple struct variables of the same type:
struct string Liming, Liuqi, ...;
There is a type of struct that often appears inside functions, which omits the struct name and is called an unnamed struct, as shown below:
struct { char name[8]; int age; char sex[2]; char depart[20]; float wage1, wage2, wage3, wage4, wage5; } Liming, Liuqi;
Accessing and Assigning Struct Members
A struct is a new data type, and the representation of struct members is:
struct_variable.member_name
If we consider “struct_variable.member_name” as a whole, it can be used like other variables.
The following example defines a struct variable stu1, assigns values to name, age, and group, and prints the output.
#include<stdio.h>int main(){struct { char *name; // Nameint age; // Age char group; // Group } stu1;// Assign values to struct members stu1.name = "Tom"; stu1.age = 18; stu1.group = 'A';// Read values of struct membersprintf("%s's age is %d, in group %c\n", stu1.name, stu1.age, stu1.group);return 0;}
Struct Arrays
A struct array is a collection of variables of the same struct type. For example, to define a class of 40 students with their names, genders, ages, and addresses, it can be defined as a struct array as follows:
struct { char name[8]; char sex[2]; int age; char addr[40]; } student[40];
Accessing members of a struct array is done using the array element as the struct variable, in the form of:
struct_array_element.member_name
For example:
student[0].name;student[30].age;
Struct Pointers
A struct pointer is defined using the * operator placed before the struct variable name. A struct pointer can be defined as follows:
struct string{ char name[8]; char sex[2]; int age; char addr[40]; }*student;
Accessing struct members using a struct pointer differs from accessing struct members using a struct variable. The access method for struct members using a struct pointer is:
struct_pointer_name->struct_member
The statements to assign values to name and age in the defined struct above are:
strcpy(student->name, "acket"); // student->name is equivalent to (*student).name;student->age=18;
It should be noted that a struct pointer points to a struct, specifically the address of the first member of the struct. Therefore, before using it, the struct pointer should be initialized, allocating memory space equal to the length of the entire struct:
student=(struct string*)malloc(sizeof(struct string));// sizeof(struct string) automatically calculates the byte length of the string struct
The malloc() function defines a memory area of the size of the struct and returns its address as a struct pointer.Bit Fields
A bit field is a special type of struct, and the general form of defining a bit field is:
struct bit_field_name { data_type variable_name: integer_constant; data_type variable_name: integer_constant; } bit_field_variable;
Here, the data type must be int (unsigned or signed, but when the member length is 1, it is considered unsigned), and the integer constant must be a non-negative integer from 0 to 15, representing the number of bits. The variable name is optional. Below is a definition of a bit field:
struct { unsigned incon: 8; /* incon occupies the low byte from 0 to 7, a total of 8 bits */ unsigned txcolor: 4;/* txcolor occupies the high byte from 0 to 3, a total of 4 bits */ unsigned bgcolor: 3;/* bgcolor occupies the high byte from 4 to 6, a total of 3 bits */ unsigned blink: 1; /* blink occupies the 7th bit of the high byte */ } ch;
Accessing members of a bit field is the same as accessing struct members. Accessing the bgcolor member in the bit field can be written as:
ch.bgcolor;
Example
struct info{ char name[8]; int age; struct addr address; float pay; unsigned state: 1; unsigned pay: 1; } workers;
The above struct defines information about wages, where there are two bit field members of 1 bit each, representing the worker’s status and whether the wage has been paid.
Defining Struct with Typedef
typedef struct person{int age;char *name;char *sex;} student;student stu1; // Here, student can be used to define a struct variable
The role of typedef is equivalent to giving struct person an alias student.