当前位置:网站首页>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
边栏推荐
- Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
- Assign the output of a command to a variable [repeat] - assigning the output of a command to a variable [duplicate]
- [Chongqing Guangdong education] National Open University autumn 2018 0088-21t Insurance Introduction reference questions
- 119. Pascal‘s Triangle II. Sol
- Oracle hint understanding
- 笔记本电脑蓝牙怎么用来连接耳机
- "Chris Richardson microservices series" uses API gateway to build microservices
- Two stage locking protocol for concurrency control
- Interprocess communication in the "Chris Richardson microservice series" microservice architecture
- Talking about MySQL index
猜你喜欢

Advantages and disadvantages of the "Chris Richardson microservice series" microservice architecture

Pl/sql basic syntax

科技云报道:算力网络,还需跨越几道坎?

航海日答题小程序之航海知识竞赛初赛

Summary of concurrency control

Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程

Three "factions" in the metauniverse

Kubernetes Administrator certification (CKA) exam notes (IV)

Learning of mall permission module

U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
随机推荐
PyGame practical project: write Snake games with 300 lines of code
Metaverse Ape获Negentropy Capital种子轮融资350万美元
FBO and RBO disappeared in webgpu
Three "factions" in the metauniverse
Learning of mall permission module
K210 learning notes (IV) k210 runs multiple models at the same time
Draw a red lantern with MATLAB
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
90后测试员:“入职阿里,这一次,我决定不在跳槽了”
Overview of database recovery
How to add new fields to mongodb with code (all)
Decorator learning 01
科技云报道:算力网络,还需跨越几道坎?
Search: Future Vision (moving sword)
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
Platformio create libopencm3 + FreeRTOS project
Business learning of mall commodity module
Talking about MySQL index
Database recovery strategy
Oracle hint understanding