Structure array definition and usage

A structure is a type of structured data that combines data of different types that are related to each other. It is composed of several members. The data type of each member can be a basic data type or a constructed type. When using a certain You must first declare a structure before constructing it as needed. The general situation for declaring a structure type is:

struct 结构名
{类型名 成员名1;
 类型名 成员名2;
 类型名 成员名3;
 ......
 类型名 成员名n;};

Definition of structure array (three forms)

1. Declare the structure type first, and then define the structure array

struct 结构名
{成员列表;
};
struct 结构名 数组名【元素个数】【,数组名【元素个数】,······】;

2. Declare the structure type and define the structure array at the same time

struct 结构名
{成员列表;
}数组名【元素个数】【,数组名【元素个数】,...】;

3. Directly define the structure array

struct 
{成员列表;
}数组名【元素个数】【,数组名【元素个数】,...】;

 example

struct books
{int bno;
char bname[20];
char field[10];
char author[10];
char publisher[30];
float price;
};
struct books book[5];

The structure array book has a total of 5 elements, book[0]-book[4], and each array element has the structure type of struct books.

Struct Array Initialization

The structure array can also be initialized at the same time as it is defined, partially or fully. The general form of structure array initialization is

struct 结构名 数组名【元素个数】={初值表列};

When initializing the structure array, it is necessary to follow the initialization rules of the array, and some of the array elements can be initialized, but each member of the structure array element must be initialized.

When all the structure arrays are initialized, the number of initial values ​​should match the number of elements of the structure array and the number of members of each array element. In order to enhance the readability of the program, it is best to use curly braces to enclose the initial value of each array element.

When initializing all elements of the structure array, the length of the array can be omitted, and the system determines the length of the array according to the amount of initialized data. like

struct books book[]={
{101,"changuage","computer","xw","rmydpress",39},
{102,"vbprogramming","computer","zys","qhdxpress",39},
{103,"java","computer","thq","qhdxpress",26}
};

According to the initial value, we can know that the length of book in the structure array is 3, that is, book has 3 elements book[0]-book[2].

Use of arrays of structures

The use of structure arrays is also carried out by referencing the members of the structure array elements. The access method of members in structure array elements is similar to the access method of structure variable members, and is referenced through the member operator ".". On the other hand, since the array elements can be referenced in the form of pointers through the array name, the reference to the members of the structure array elements can have the following concentrated forms:

1. Structure array name [subscript].Member name

2. (*(structure array name + subscript)).Member name

3. (structure array name + subscript) -> member name

Among them, the outermost brackets in Form 2 are indispensable, because the operator "." has a higher priority than "*". If no brackets are added, "*(structure array name + subscript). Member name" is equivalent to " *((structure array name + subscript). member name)", the meaning has changed, resulting in an error.

The "->" in 3 is called the pointing member operator, its priority is the same as the member operator ".", and the combination direction is from left to right.

Structure array members are used in the same way as structure variables of the same type.

#include<stdio.h>
struct books {
	int bno;
	char bname[20], field[10], author[10], publisher[30];
	float price;
};
int main()
{
	struct books book[5] = {
		{101,"clanguage","computer","xw","rmydpress",39},
		{102,"vbprogramming","computer","zys","qhdxpress",39},
		{103,"jave","computer","mj","jxgypress",29.8},
		{104,"linearalgebra","math","chc","dzgypress",34},
		{105,"clanguage","computer","thq","qhdxpress",26}
	};
	printf("%s\t %.2f\n", book[2].bname, book[2].price + 10);
	printf("%s\t %.2f\n", (*(book + 2)).bname, (*(book + 2)).price + 10);
	printf("%s\t %.2f\n", (book + 2)->bname, (book + 2)->price + 10);
	return 0;
}

Running results: Description, for the array structure book defined in the program,

book[2], bname, (*(book+2)).bname, (book+2)->bname all represent the member bname of the third element book[2] of the structure array book, in the printf() function Use these three forms to output the result of adding 10 to the member bname of book[2] and the value of member price of book[2] respectively.

Guess you like

Origin blog.csdn.net/m0_75115696/article/details/132692772