当前位置:网站首页>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 name
Such 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
边栏推荐
- Create a virtual machine on VMware (system not installed)
- Technology cloud report won the special contribution award for the 10th anniversary of 2013-2022 of the "cloud Ding Award" of the global cloud computing conference
- 如何快速体验OneOS
- K210学习笔记(四) K210同时运行多个模型
- Leetcode simple question check whether all characters appear the same number of times
- What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
- Business learning of mall order module
- Official clarification statement of Jihu company
- 344. Reverse String. Sol
- Sparse array [matrix]
猜你喜欢
Nacos 的安装与服务的注册
90后测试员:“入职阿里,这一次,我决定不在跳槽了”
K210学习笔记(四) K210同时运行多个模型
Technology cloud report won the special contribution award for the 10th anniversary of 2013-2022 of the "cloud Ding Award" of the global cloud computing conference
[Yugong series] go teaching course in July 2022 004 go code Notes
Leetcode simple question ring and rod
Nacos installation and service registration
What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
Web3为互联网带来了哪些改变?
Alternating merging strings of leetcode simple questions
随机推荐
Type of fault
AD637使用筆記
How can Bluetooth in notebook computer be used to connect headphones
Bitbucket installation configuration
Blocking of concurrency control
What about data leakage? " Watson k'7 moves to eliminate security threats
Cobaltstrike builds an intranet tunnel
Promql demo service
The statistics of leetcode simple question is the public string that has appeared once
Nacos 的安装与服务的注册
如何向mongoDB中添加新的字段附代码(全)
Ad637 notes d'utilisation
Go语言学习教程(十五)
AD637 usage notes
FBO and RBO disappeared in webgpu
Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
Performance testing of software testing
Form artifact
[Chongqing Guangdong education] National Open University autumn 2018 0088-21t Insurance Introduction reference questions
微服務鏈路風險分析