当前位置:网站首页>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
边栏推荐
- 科技云报道:算力网络,还需跨越几道坎?
- Business learning of mall commodity module
- database mirroring
- Shelved in TortoiseSVN- Shelve in TortoiseSVN?
- AD637使用笔记
- Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
- Livelocks and deadlocks of concurrency control
- Two stage locking protocol for concurrency control
- Storage optimization of performance tuning methodology
- Implementation technology of recovery
猜你喜欢
Oracle advanced query
50. Pow(x, n). O(logN) Sol
Storage optimization of performance tuning methodology
Alternating merging strings of leetcode simple questions
Sparse array [matrix]
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
AD637 usage notes
Server optimization of performance tuning methodology
[Yugong series] go teaching course in July 2022 004 go code Notes
How can Bluetooth in notebook computer be used to connect headphones
随机推荐
Promql demo service
MySQL actual combat 45 lecture learning (I)
[agc009e] eternal average - conclusion, DP
如何创建线程
Hcip day 16
Pl/sql basic case
Oracle is sorted by creation time. If the creation time is empty, the record is placed last
What about data leakage? " Watson k'7 moves to eliminate security threats
Win11运行cmd提示“请求的操作需要提升”的解决方法
K210学习笔记(四) K210同时运行多个模型
Metaverse Ape上线倒计时,推荐活动火爆进行
Ad637 notes d'utilisation
Daily question brushing record (XIV)
Recovery technology with checkpoints
了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
Postman核心功能解析-参数化和测试报告
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
The statistics of leetcode simple question is the public string that has appeared once
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
Two stage locking protocol for concurrency control