structural morphology
Form of structure
-
struct structure name
{
Structural member 1;
Structural member 2;
...
};
For example: Book Structure
struct Book { char title[128]; char author[48]; float price; unsigned int date; char publisher[40]; };
-
Defining structure type variables
You can follow the variables when the structure is declared, which are global variables
for example
struct Book { char title[128]; char author[48]; float price; unsigned int date; char publisher[40]; }book;//boo is a global variable
It can also be defined in the main function
#include <stdio.h> struct Book { char title[128]; char author[48]; float price; unsigned int date; char publisher[40]; }; int main(void) { struct Book book;//This is a local variable return 0; }
-
Accessing structure variables
To access structure members, we need to introduce a new operator, the dot (.) operator. For example, book.title refers to the title member of the book structure, which is a character array; book.price refers to the price member of the book structure, which is a floating-point variable
For example, books
#include <stdio.h> struct Book { char title[128]; char author[48]; float price; unsigned int date; char publisher[40]; }book; int main(void) { printf("Please enter the title of the book: "); scanf("%s", book.title); printf("Please enter author: "); scanf("%s", book.author); printf("Please enter the selling price: "); scanf("%f", &book.price); printf("Please enter publication date: "); scanf("%d", &book.date); printf("Please enter the publishing house: "); scanf("%s", book.publisher); printf("\n=======Data entry completed=====\n"); printf("Title: %s\n", book.title); printf("author: %s\n", book.author); printf("price: %.2f\n", book.price); printf("Publication date: %d\n", book.date); printf("Press: %s\n", book.publisher); return 0; }
Result output
Please input the title of the book: the old man and the sea Please enter Author: Hemingway Please enter the selling price: 20 Please enter publication date: 20200113 Please enter Publishing House: China University of mining and Technology Press =======Data entry completed===== Title: the old man and the sea By Hemingway Price: 20.00 Publication date: 20200113 Press: China University of mining and Technology Press
Initializing structure variables
Initialize a struct variable:
struct Book book = { "The old man and the sea", "Hemingway"; 20, 20200113 "China University of mining and Technology Press" };
Be sure to take your seat
Initializes the specified member value of the structure
Struct initializer uses dot (.) operator and member name
For example: let the program only initialize the price member of Book:
struct Book book = {.price = 20};
You can also initialize without following the member order declared by the structure:
struct Book book = { .publisher = "China University of mining and Technology Press", .price = 20, .date = 20200113 };
Nested structure
Nesting example:
#include <stdio.h> struct Date { int year; int month; int day; }; struct Book { char title[128]; char author[40]; float price; struct Date date;//nesting char publisher[40]; }book = { "The old man and the sea", "Hemingway", 20, {2020, 01, 13}, "China University of mining and Technology Press" }; int main(void) { printf("Title: %s\n", book.title); printf("author: %s\n", book.author); printf("Price: %.2f\n", book.price); printf("date: %d-%d-%d\n", book.date.year, book.date.month, book.date.day); printf("Press: %s\n", book.publisher); return 0; }
Output result
Title: the old man and the sea By Hemingway Price: 20.00 Date: January 13, 2020 Press: China University of mining and Technology Press
Structure array
The first method is defined when the structure is declared:
struct structure name { Structural member 1; Structural member 2; ... }Array name [length];
The second method is to first declare a structure type, and then define a structure array with this type:
struct structure name { Structural member 1; Structural member 2; ... }; struct structure name array name [length];
Initialize structure array
Example:
struct Book book[3] = { {"The old man and the sea", "Hemingway", 20, {2020, 01, 13}, "China University of mining and Technology Press"}; {"The old man and the land", "Lu Ming Wei", 21, {2021, 01, 13}, "tsinghua university press "}; {"Old man and empty", "Kong Ming Wei", 22, {2022, 01, 13}, "Peking University Press"}; };
Transfer structure variable
- Information can be transferred between structural variables through assignment numbers, provided that the types of variables are the same;
Example:
#include <stdio.h> int main(void) { struct Test { int x; int y; }t1, t2; t1.x = 3; t1.y = 4; t2 = t1;//Assignment number transfer printf("t2.x = %d, t2.y = %d", t2.x, t2.y); return 0; }
Output result
t2.x = 3, t2.y = 4
Structure pointer
Example: book entry procedure
#include <stdio.h> struct Date { int year; int month; int day; }; struct Book { char title[128]; char author[40]; float price; struct Date date; char publisher[40]; }; struct Book getInput(struct Book book); void printBook(struct Book book); struct Book getInput(struct Book book) { printf("Please enter the title of the book: "); scanf("%s", book.title); printf("Please enter author: "); scanf("%s", book.author); printf("Please enter the selling price: "); scanf("%f", &book.price); printf("Please enter publication date: "); scanf("%d-%d-%d", &book.date.year, &book.date.month, &book.date.day); printf("Please enter the publishing house: "); scanf("%s", book.publisher); return book; }; void printBook(struct Book book) { printf("Title: %s\n", book.title); printf("author: %s\n", book.author); printf("price: %.2f\n", book.price); printf("Publication date: %d-%d-%d\n", book.date.year,book.date.month,book.date.day); printf("Press: %s\n", book.publisher); } int main(void) { struct Book b1, b2; printf("Please enter the information of the first book...\n"); b1 = getInput(b1); putchar('\n'); printf("Please enter the information of the second book...\n"); b2 = getInput(b2); printf("\n\n Input completed,Print verification now...\n\n"); printf("Print information for the first book..\n"); printBook(b1); putchar('\n'); printf("Print information for the second book..\n"); printBook(b2); return 0; }
Output result
Please enter the information of the first book Please input the title of the book: the old man and the sea Please enter Author: Hemingway Please enter the selling price: 20 Please enter publication date: January 13, 2020 Please enter: Tsinghua University Press Please enter the information of the second book Please enter the title of the book: young people and the sea Please enter Author: Hemingway Please enter the selling price: 20 Please enter publication date: January 13, 2020 Please enter: Peking University Press After input, start printing verification now Print the first book Title: the old man and the sea By Hemingway Price: 20.00 Publication date: January 13, 2020 Press: Tsinghua University Press Print information for the second book Title: young people and the sea By Hemingway Price: 20.00 Publication date: January 13, 2020 Press: Peking University Press
Now use structure pointer
Or the above example, slightly modified
#include <stdio.h> struct Date { int year; int month; int day; }; struct Book { char title[128]; char author[40]; float price; struct Date date; char publisher[40]; }; void getInput(struct Book *book); void printBook(struct Book *book); void getInput(struct Book *book) { printf("Please enter the title of the book: "); scanf("%s", book->title); printf("Please enter author: "); scanf("%s", book->author); printf("Please enter the selling price: "); scanf("%f", &book->price); printf("Please enter publication date: "); scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day); printf("Please enter the publishing house: "); scanf("%s", book->publisher); }; void printBook(struct Book *book) { printf("Title: %s\n", book->title); printf("author: %s\n", book->author); printf("price: %.2f\n", book->price); printf("Publication date: %d-%d-%d\n", book->date.year,book->date.month,book->date.day); printf("Press: %s\n", book->publisher); } int main(void) { struct Book b1, b2; printf("Please enter the information of the first book...\n"); getInput(&b1); putchar('\n'); printf("Please enter the information of the second book...\n"); getInput(&b2); printf("\n\n Input completed,Print verification now...\n\n"); printf("Print information for the first book..\n"); printBook(&b1); putchar('\n'); printf("Print information for the second book..\n"); printBook(&b2); return 0; }
Output result
Print the first book Title: the old man and the sea By Hemingway Price: 20.00 Publication date: January 13, 2020 Press: Tsinghua University Press Print information for the second book Title: young people and the sea By Hemingway Price: 20.00 Publication date: January 13, 2020 Press: Peking University Press
Published 9 original articles, won praise 1, visited 1051