当前位置:网站首页>C language - structural basis
C language - structural basis
2022-07-05 22:18:00 【DSTBP】
Structure foundation
Arrays allow you to define storable identical Variables of type data items ,
structure Is another user-defined available data type , It allows you to store Different Data item of type .
One 、 explain
C In language , The structure type belongs to a construction type ( Other construction types are : An array type , Joint type )
lead :
In practical terms , A set of data often has many different data types . for example , Register student information , You may need to use char Type name ,int Type or char Type student number ,int Type of age ,char Type of gender ,float Type achievement .
Structure ( similar “ Record ”), Can effectively solve this problem .
Concept
Structure is essentially a data type , But it can include several “ member ”, The type of each member can be the same or different , It can also be a basic data type or a construction type .
advantage
Structures can not only record different types of data , And make the data structure “ High cohesion , Low coupling ” Of , It is more conducive to the reading, understanding and transplantation of the program , And the storage mode of the structure can improve CPU Access speed to memory .
Two 、 Statement
struct tag
{
member-list
member-list
member-list
...
} variable-list ; // Don't forget that there is a semicolon after the braces
analysis :
struct The keyword represents a structure .
tag It's the structure label .
member-list Member list , It's the standard variable definition , such as int i; perhaps float f, Or other valid variable definitions .
variable-list Structural variables , The definition is at the end of the structure , Before the last semicolon , You can specify one or more structure variables .
Structure declarations can be placed outside functions ( This is the global structure , Similar to global variables , All functions declared after it can use )
You can also put it in a function ( This is the local structure , Similar to local variables , Can only be used inside this function , If it has the same name as the global structure , The global structure will be temporarily masked ).
This declaration only defines the structure type ( What is in the structure int char float), There are no variables defined (std1 std2)
example
// For example, declare the structure of a student : struct Student { char name[20]; // full name int num; // Student number float score; // achievement }; // Pay attention to the semicolon
3、 ... and 、 Structural variables
The definition of structure does not allocate storage space , Structural variables are allocated corresponding storage according to their data structures
struct Structure name Structure variable name ;
example
struct Student
{
char name[20]; // full name
int num; // Student number
float score; // achievement
};
struct Student stu1; // Define structure variables
struct Student
{
char name[20];
int num;
float score;
}stu1;
// The definition is followed by the variable name
After defining structure variables , The system will allocate memory units , The occupied length is the sum of bytes occupied by variables in the structure , The specific length can be used in the compiler sizeof Find out the keywords respectively .
Four 、 Different forms of structure
tag、member-list、variable-list this 3 At least part of it must appear 2 individual .
The structure is not labeled
struct { int a; char b; double c; } s1;This method cannot define new structural variables again (s2,s3…) 了 .
The structure does not declare variables
struct example { int a; char b; double c; };Labeled structure , Variable declared t1、t2、t3
struct SIMPLE t1, t2[20], *t3;use typedef Create a new type
typedef struct { int a; char b; double c; } Simple2; // Now we can use Simple2 Declare new struct variables as types Simple2 u1, u2[20], *u3;
5、 ... and 、 Access structure members
Point with the structure member operator (.)
General form of accessing members :
Structure variable name . Member nameSuch as stu1 . name Students stu1 The name of .
The member in the structure is another structure The form of interview :
Structure variable name . Structure variable name (…) . Member name
Such as stu1.birthday.year Visit the year of birthstruct Birthday { int year; int month; int day; }; struct Student { char name[20]; int num; float score; struct Birthday birthday; // Birthday }stu1; struct Student { char name[20]; int num; float score; struct Birthday{ int year; int month; int day; } bir1; }stu1;Specific operation
example#include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main( ) { struct Books Book1; /* Statement Book1, The type is Books */ struct Books Book2; /* Statement Book2, The type is Books */ /* Book1 detailed */ strcpy( Book1.title, "C Programming"); strcpy( Book1.author, "Nuha Ali"); strcpy( Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* Book2 detailed */ strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Zara Ali"); strcpy( Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* Output Book1 Information */ printf( "Book 1 title : %s\n", Book1.title); printf( "Book 1 author : %s\n", Book1.author); printf( "Book 1 subject : %s\n", Book1.subject); printf( "Book 1 book_id : %d\n", Book1.book_id); /* Output Book2 Information */ printf( "Book 2 title : %s\n", Book2.title); printf( "Book 2 author : %s\n", Book2.author); printf( "Book 2 subject : %s\n", Book2.subject); printf( "Book 2 book_id : %d\n", Book2.book_id); return 0; }
6、 ... and 、 Initialization of structure variables
- The definition is assigned directly
struct Student { char name[20]; char sex; int number; }stu1 = { "zhaozixuan",'M',12345}; // Note that the type and order of initialization values should be consistent with the type and order of members when the structure is declared * struct Student { char name[20]; char sex; int number; }; struct Student stu1 = { "zhaozixuan",'M',12345}; - After defining the structure, assign values one by one
strcpy(stu1.name, " Wang Wei "); stu1.sex = 'M'; stu1.number = 12305; - Assign values in any order after definition
struct Student stu1 = { .name = "Wang", .number = 12345, .sex = 'W', }; - Partial initialization
struct Student stu4 = { .name = "Lisa"}; - Initialize a new structure variable of the same type with an existing structure , Overall copy ( Each member is assigned to the new structure variable one by one )
stu3 = stu1;
Be careful
When initializing structure variables , To assign values to structure members one by one , Cannot skip previous member variables , And directly assign initial values to the following members , But you can only assign the first few , For the following unassigned variables , If it's numerical , It will be automatically assigned to 0, For character type , The initial value will be automatically assigned to NULL, namely ‘\0’.
7、 ... and 、 References to structure variables ( Output and input )
. It's the operator , Highest priority of all operators
printf("%d",stu1.name);
scanf("%d",&stu2.birthday.month);
If the member of the structure itself is a structure , You need to continue to use . Operator , Up to the lowest level .(stu2.birthday.month) correct (stu1.birthday) error
边栏推荐
- The simple problem of leetcode is to split a string into several groups of length K
- C language knowledge points link
- MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
- Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
- Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
- 2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
- [Yugong series] go teaching course in July 2022 004 go code Notes
- Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
- Leetcode simple question: the minimum cost of buying candy at a discount
- "Chris Richardson microservices series" uses API gateway to build microservices
猜你喜欢

Granularity of blocking of concurrency control

Cobaltstrike builds an intranet tunnel

A substring with a length of three and different characters in the leetcode simple question

元宇宙中的三大“派系”

The real situation of programmers

Livelocks and deadlocks of concurrency control

Database tuning solution

database mirroring

Pl/sql basic case

Overview of database recovery
随机推荐
Evolution of large website architecture and knowledge system
Daily question brushing record (XIV)
C language knowledge points link
等到产业互联网时代真正发展成熟,我们将会看待一系列的新产业巨头的出现
Decorator learning 01
阿龙的感悟
Recovery technology with checkpoints
Hcip day 16
Web3为互联网带来了哪些改变?
Metaverse Ape上线倒计时,推荐活动火爆进行
Draw a red lantern with MATLAB
The new content of the text component can be added through the tag_ Config set foreground and background colors
K210学习笔记(四) K210同时运行多个模型
Ad637 notes d'utilisation
Livelocks and deadlocks of concurrency control
The statistics of leetcode simple question is the public string that has appeared once
微服務鏈路風險分析
AD637 usage notes
Platformio create libopencm3 + FreeRTOS project
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用