当前位置:网站首页>Composite type (custom type)
Composite type (custom type)
2022-07-03 14:52:00 【Min, Hello, Xun】
List of articles
Structure
summary
- Array : Describes an ordered set of data of the same type , Used to process a large number of data operations of the same type .
- Sometimes we need to combine different types of data into an organic whole , Such as : A student has a student number / full name / Gender / Age / Address and other properties . Obviously, it is tedious to define the above variables alone , Data is not easy to manage .
- C Language gives another type of construction data —— Structure .
Structure variable definition and initialization
How to define structure variables :
- First declare the structure type and then define the variable name
- Define variables while declaring types
- Directly define structure type variables ( No type name )
Example :
Method 1 :
#include<stdio.h>
//struct Structure name
//{
// // Structure member list
// full name
// Age
//};
struct student
{
char name[21];
int age;
int score;
char addr[51];
};
int main()
{
// Create and structure variables
// Method 1: It's slow
//struct student stu;// Type of structure Structural variable
//strcpy(stu.name," Zhang San ");// When the struct assigns a value to a string array , Use copy assignment .// The array name is constant , Therefore, you cannot use =, Have to use strcpy
//stu.age = 18;
//stu.score = 100;
//strcpy(stu.addr, " Taiyuan City, Shanxi Province ");
// Method 2
struct student stu = {
" Zhang San ",18,100," Taiyuan City, Shanxi Province " };
printf(" full name :%s\n", stu.name);
printf(" Age :%d\n", stu.age);
printf(" achievement :%d\n", stu.score);
printf(" Address :%s\n", stu.addr);
return 0;
}
Method 2 :
#include<stdio.h>
struct student
{
char name[21];
int age;
int score;
char addr[51];
}stu= {
" Zhang San ",18,100," Taiyuan City, Shanxi Province " };
int main0202()
{
printf(" full name :%s\n", stu.name);
printf(" Age :%d\n", stu.age);
printf(" achievement :%d\n", stu.score);
printf(" Address :%s\n", stu.addr);
return 0;
}
int main(void)
{
struct student stu;
// Keyboard get value
scanf("%s%d%d%s", stu.name, &stu.age, &stu.score, stu.addr);// Array name first address , So don't add &
printf(" full name :%s\n", stu.name);
printf(" Age :%d\n", stu.age);
printf(" achievement :%d\n", stu.score);
printf(" Address :%s\n", stu.addr);
return 0;
}
result :
full name : Zhang San
Age :18
achievement :100
Address : Taiyuan City, Shanxi Province
Array of structs
Example :
The header file .h——struct.h
#pragma once
struct student
{
char name[21];
int age;
char sex;
int score[3];
char addr[51];
};
Main function call header file
#include<stdio.h>
#include "struct.h"// Header file call
int main(void)
{
struct student stu[3]=
{
{
" Ma Xiaoxiao ",19,'M',90,100,98," Zhengzhou, Henan "},
{
" Ding Xin ",18,'F',97,96,99," Chongqing "},
{
" Liu guangtou ",15,'M',97,96,98," Chongqing "}
};
printf(" Structure size :%d\n", sizeof(stu));
printf(" Structure element size :%d\n", sizeof(stu[0])); Structure members need offset alignment
printf(" Number of structural elements :%d\n", sizeof(stu) / sizeof(stu[0]));
for (int i = 0; i < 3; i++)
{
printf(" full name :%s\n", stu[i].name);
printf(" Age :%d\n", stu[i].age);
printf(" Gender :%s\n", stu[i].sex == 'M' ? " male " : " Woman ");
printf(" achievement 1:%d\n", stu[i].score[0]);
printf(" achievement 2:%d\n", stu[i].score[1]);
printf(" achievement 3:%d\n", stu[i].score[2]);
printf(" Address :%s\n", stu[i].addr);
printf("\n");
}
return 0;
}
result :
Structure size :288
Structure element size :96
Number of structural elements :3
full name : Ma Xiaoxiao
Age :19
Gender : male
achievement 1:90
achievement 2:100
achievement 3:98
Address : Zhengzhou, Henan
full name : Ding Xin
Age :18
Gender : Woman
achievement 1:97
achievement 2:96
achievement 3:99
Address : Chongqing
full name : Liu guangtou
Age :15
Gender : male
achievement 1:97
achievement 2:96
achievement 3:98
Address : Chongqing
Structure ordering
#include<stdio.h>
#include "struct.h"// Header file call
// Structure ordering
int main(void)
{
struct student stu[3] =
{
{
" Ma Xiaoxiao ",19,'M',90,100,98," Zhengzhou, Henan "},
{
" Ding Xin ",18,'F',97,96,99," Chongqing "},
{
" Liu guangtou ",15,'M',97,96,98," Chongqing "}
};
for (int i = 0; i < 3 - 1; i++)
{
for (int j = 0; j < 3 - i - 1; j++)
{
if (stu[j].age > stu[j + 1].age)
{
struct student temp=stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
for (int i = 0; i < 3; i++)
{
printf(" full name :%s\n", stu[i].name);
printf(" Age :%d\n", stu[i].age);
printf(" Gender :%s\n", stu[i].sex == 'M' ? " male " : " Woman ");
printf(" achievement 1:%d\n", stu[i].score[0]);
printf(" achievement 2:%d\n", stu[i].score[1]);
printf(" achievement 3:%d\n", stu[i].score[2]);
printf(" Address :%s\n", stu[i].addr);
printf("\n");
}
return 0;
}
result :
full name : Liu guangtou
Age :15
Gender : male
achievement 1:97
achievement 2:96
achievement 3:98
Address : Chongqing
full name : Ding Xin
Age :18
Gender : Woman
achievement 1:97
achievement 2:96
achievement 3:99
Address : Chongqing
full name : Ma Xiaoxiao
Age :19
Gender : male
achievement 1:90
achievement 2:100
achievement 3:98
Address : Zhengzhou, Henan
Open up heap space storage structure
Example :
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include "struct.h"
typedef struct student ss;// Name it
int main(void)
{
//printf("%d", sizeof(struct student));// Structure element size
ss* p = (ss*)malloc(sizeof(ss) * 3);// Open up space
for (int i = 0; i < 3; i++)
{
scanf("%s%d,%c%d%d%d%s",p[i].name,&p[i].age,&p[i].sex,
&p[i].score[0],&p[i].score[1],&p[i].score[2],p[i].addr);// Because there are characters "%d%c", So add a comma to distinguish the previous int Type and character type
}
for (int i = 0; i < 3; i++)
{
printf(" full name :%s\n", p[i].name);
printf(" Age :%d\n", p[i].age);
printf(" Gender :%s\n", p[i].sex == 'M' ? " male " : " Woman ");
printf(" achievement 1:%d\n", p[i].score[0]);
printf(" achievement 2:%d\n", p[i].score[1]);
printf(" achievement 3:%d\n", p[i].score[2]);
printf(" Address :%s\n", p[i].addr);
printf("\n");
}
free(p);
return 0;
}
result :
ha-ha 18,M 89 98 99 shanxi
Hee hee 19,M 99 80 98 shaanxi
Hey 17,M 78 85 86 Shandong
full name : ha-ha
Age :18
Gender : male
achievement 1:89
achievement 2:98
achievement 3:99
Address : shanxi
full name : Hee hee
Age :19
Gender : male
achievement 1:99
achievement 2:80
achievement 3:98
Address : shaanxi
full name : Hey
Age :17
Gender : male
achievement 1:78
achievement 2:85
achievement 3:86
Address : Shandong
Structure nested structure
Example :
Create header file .h file
struct1.h
#pragma once
struct scores
{
int cl;//c Language
int cpp;//c++
int cs;//c#
};
struct student
{
char name[21];
int age;
struct scores ss;
char addr[51];
};
Main program call
#include <stdio.h>
#include "struct1.h"
int main(void)
{
struct student stu = {
" hello ",20,98,99,100," Baoji, Shaanxi " };
printf("%s\n%d\n%d\n%d\n%d\n%s", stu.name, stu.age, stu.ss.cl, stu.ss.cpp, stu.ss.cs, stu.addr);
return 0;
}
result :
hello
20
98
99
100
Baoji, Shaanxi
Structure assignment
Example :
#include<stdio.h>
#include"struct.h"
int main()
{
struct student stu = {
" Whoosh ",19,89,98,100," taiyuan " };
struct student s1 = stu;
// The array name is constant , Therefore, you cannot use =, Have to use strcpy
strcpy(s1.name, " ha-ha ");// Yes s1 The modification of does not affect stu Although copy But the two are independent spaces
printf("%s", stu.name);
return 0;
}
result :
Whoosh
Structures and pointers
The structure member is of pointer type
Example :
#include<stdio.h>
struct student// The structure member is of pointer type
{
char* name;
int age;
int* scores;
char* addr;
};
int main()
{
struct student stu;
// Open up space
stu.name = (char*)malloc(sizeof(char) * 21);// Is a pointer , You need to open up heap space and then assign values , Otherwise, the value is constant and cannot be changed
stu.scores = (int*)malloc(sizeof(int) * 3);
stu.addr = (char*)malloc(sizeof(char) * 51);
// Structure assignment
strcpy(stu.name, " Li Si ");
stu.age = 18;
stu.scores[0] = 89;
stu.scores[1] = 89;
stu.scores[2] = 89;
strcpy(stu.addr, " Chongqing ");
// Print
printf("%s\n", stu.name);
printf("%d\n", stu.age);
printf("%d\n", stu.scores[0]);
printf("%d\n", stu.scores[1]);
printf("%d\n", stu.scores[2]);
printf("%s\n", stu.addr);
// Free heap space
free(stu.name);
free(stu.scores);
free(stu.addr);
return 0;
}
result :
Li Si
18
89
89
89
Chongqing
Structure pointer
#include<stdio.h>
int main()
{
struct student1 ss = {
" Wang Wu ",17,84,85,86," sichuan " };
struct student1* p = &ss;
/*printf("%s\n", (*p).name); printf("%d\n", (*p).age); printf("%d\n", (*p).scores[0]);*/ //==
// Structure pointer -> member
// Structural variable . member
printf("%s\n", p->name);
printf("%D\n", p->scores[0]);
return 0;
}
result :
Wang Wu
84
Structural integration ( It is similar to opening up the heap space opened up by the secondary pointer )
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
typedef struct student ss;
struct student
{
char* name;
int age;
int* scores;
char* addr;
};
int main()
{
ss* p = (ss*)malloc(sizeof(ss) * 3);
for (int i = 0; i < 3; i++)
{
p[i].name= (char*)malloc(sizeof(char) * 21);
p[i].scores = (int*)malloc(sizeof(int) * 3);
p[i].addr = (char*)malloc(sizeof(char) * 51);
}
for (int i = 0; i < 3; i++)
{
scanf("%s%d%d%d%d%s", p[i].name, &p[i].age, &p[i].scores[0],
&p[i].scores[1], &p[i].scores[2], p[i].addr);
}
for (int i = 0; i < 3; i++)
{
printf("%s %d %d %d %d %s\n", p[i].name, p[i].age, (p+i)->scores[0],
(p+i)->scores[1], p[i].scores[2], p[i].addr);
}
// Release
for (int i = 0; i < 3; i++)
{
free(p[i].name);
free(p[i].scores);
free(p[i].addr);
}
free(p);
return 0;
}
result :
ha-ha 18 98 99 96 Beijing
ha-ha 17 95 96 97 Shanghai
Hee hee 16 89 86 87 Guangzhou
ha-ha 18 98 99 96 Beijing
ha-ha 17 95 96 97 Shanghai
Hee hee 16 89 86 87 Guangzhou
Structure and function
Example :
# include<stdio.h>
#include "struct.h"
typedef struct studennt ss;
void fun01(struct student stu1)
{
strcpy(stu1.name, " Hey ");// Change the parameters
printf("%s\n", stu1.name);
}
// If the structure member is a pointer , You need to open up heap space in the main function , If the formal parameters in other functions do not open up heap space
// be , Changes to formal parameters will affect arguments .
int main0901()
{
struct student stu = {
" ha-ha ",18,89,79,99," tianjin " };
fun01(stu);
printf("%s", stu.name);// Formal parameters do not change arguments
return 0;
}
void fun02(struct student* p)
{
strcpy(p->name, " Hello ");// Address delivery , You can change arguments
printf("%s\n", p->name);
}
int main0902()
// Structure pointer as function parameter
{
struct student stu = {
" Hee hee ",19,98,79,97," nanjing " };
fun02(&stu);
printf("%s", stu.name);
return 0;
}
// Arrays degenerate into pointers as function parameters Missing element precision , You need to pass the number
void BUbbleSort(struct student* stu, int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (stu[j].age > stu[j + 1].age)
{
struct student temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
printf("\n");
}
}
int main()
{
// Structure array as function parameter
struct student stu[3] =
{
{
" Zhang San ",20,89,89,89," baoji "},
{
" Li Si ",18,87,87,87," Hanzhong city "},
{
" Wang Wu ",21,85,85,85," Weinan "}
};
BUbbleSort(stu, 3);
for (int i = 0; i < 3; i++)
{
printf(" full name :%s\n", stu[i].name);
printf(" Age :%d\n", stu[i].age);
printf(" achievement 1:%d\n", stu[i].score[0]);
printf(" achievement 2:%d\n", stu[i].score[1]);
printf(" achievement 3:%d\n", stu[i].score[2]);
printf(" Address :%s\n", stu[i].addr);
printf("\n");
}
return 0;
}
result 0901:
Hey
ha-ha
result 0902:
Hello
Hello
result 0903:
full name : Li Si
Age :18
achievement 1:87
achievement 2:87
achievement 3:8949408
Address :
full name : Zhang San
Age :20
achievement 1:89
achievement 2:89
achievement 3:8949400
Address :
full name : Wang Wu
Age :21
achievement 1:85
achievement 2:85
achievement 3:8949416
Address :
const Decorating structure pointer
Example :
#include<stdio.h>
typedef struct student ss;
struct student
{
char name[21];
int age;
char sex;
int score[3];
char addr[51];
};
int main1001()
{
ss stu1 = {
" Chen Yi ",22,99,99,99," their " };
ss stu2 = {
" Guo er ",24,98,98,98," tianshui " };
//const Decorate the structure pointer type , Modifiable variables Cannot modify the value of pointer pointing to memory space
const ss* p = &stu1;
//p = &stu2;//ok
//(*p).age = 28;//err
//p->age = 30;//err
return 0;
}
int main1002()
{
ss stu1 = {
" Chen Yi ",22,99,99,99," their " };
ss stu2 = {
" Guo er ",24,98,98,98," tianshui " };
//const Modify structure pointer variables
ss* const p = &stu1;
//strcpy(p->name, " Sun San ");//ok
//p = &stu2;//err
//(*p).age = 28;//ok
//p->age = 30;//ok
return 0;
}
//const Decorate the structure pointer type ,const Modify structure pointer variables
int main1001()
{
ss stu1 = {
" Chen Yi ",22,99,99,99," their " };
ss stu2 = {
" Guo er ",24,98,98,98," tianshui " };
//const Modify structure pointer variables
const ss* const p = &stu1;
//p = &stu2;//err
//(*p).age = 28;//err
ss** pp = &p;
//(*pp)->age = 45;//ok The pointer points to the content with ->
//*pp = &stu2;//ok
//(**pp).age = 50;//ok Variable . Use .
return 0
}
Consortium
- union union It is a type that can store different types of data in the same storage space ;
- The memory length occupied by the consortium is equal to multiple of the length of its longest member , It is also called a common body ;
- The same memory segment can be used to store several different types of members , But there's only one thing that works in every instant ;
- The active member in the community variable is the last member stored , After saving a new member, the value of the original member will be overwritten ;
- The address of a common variable is the same as that of its members .
Example :
#include<stdio.h>
union Var // The addresses of all types in the consortium are the same
{
int a;
float b;
double c;
char d;
short f;
};
int main()
{
union Var var;
var.a = 10;
var.b = 3.14;
printf("%d\n",var.a);
printf("%f\n", var.b);// There is only one effect in every instant , The active member is the last stored member
printf("%d\n", sizeof(var));// The size is calculated according to the type with the largest memory
return 0;
}
result :
1078523331
3.140000
8
enumeration
- enumeration : List the values of variables one by one , The value of a variable is limited to the values listed .
- Enumerations are often used for process control
- Enumeration type definition :
Example :
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
// Enumerations are often used for process control
enum TYPE
{
run,attack,skill,dance=10,showUI,frozen=20,dizz,dath,moti
}type;
//enum transaction
//{
// Card insertion , card reading , Lock card , Input password , Inquire about , Withdraw money , Withdraw card , Pick up the card
//};
int main()
{
int value;
while (1)// Dead cycle
{
scanf("%d", &value);
switch (value)//switch Write... In brackets type Is to show case type , Write value It is to output various types
{
case run:
printf(" The hero is moving .....\n");
break;
case attack:
printf(" The hero is attacking .....\n");
break;
case skill:
printf(" Heroes are releasing skills .....\n");
break;
case dance:
printf(" The hero is dancing .....\n");
break;
case showUI:
printf(" The hero is displaying the badge .....\n");
break;
case frozen:
printf(" Heroes are frozen .....\n");
break;
case dizz:
printf(" The hero is dizzy .....\n");
break;
case dath:
printf(" The hero dies .....\n");
return 0;// The outer layer is an endless loop ,break Can't jump out , You can add goto
break;
case moti:
printf(" The hero waits for the release order .....\n");
break;
default:
break;
}
}
return 0;
}
result :
0
The hero is moving .....
1
The hero is attacking .....
10
The hero is dancing .....
typedef keyword
- effect : names Define function pointers
- typedef by C Keywords of language , The function is for a data type ( Basic type or custom data type ) Define a new name , Cannot create new type .
- And #define Different ,typedef Data types only , Instead of an expression or a specific value .
- #define Occurs during preprocessing ,typedef It happens in the compilation phase .
边栏推荐
- Time conversion ()
- [ue4] geometry drawing pipeline
- Yolov5进阶之八 高低版本格式转换问题
- dllexport和dllimport
- 零拷贝底层剖析
- 556. The next larger element III: simple construction simulation questions
- Talking about part of data storage in C language
- Chapter 14 class part 1
- [opengl] pre bake using computational shaders
- Code writing and playing method of tonybot humanoid robot at fixed distance
猜你喜欢
亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
[ue4] material and shader permutation
Niuke: crossing the river
Use of form text box (I) select text
[ue4] cascading shadow CSM
dllexport和dllimport
Detailed explanation of four modes of distributed transaction (Seata)
The latest M1 dedicated Au update Adobe audit CC 2021 Chinese direct installation version has solved the problems of M1 installation without flash back!
My QT learning path -- how qdatetimeedit is empty
Amazon, express, lazada, shopee, eBay, wish, Wal Mart, Alibaba international, meikeduo and other cross-border e-commerce platforms evaluate how Ziyang account can seize traffic by using products in th
随机推荐
关于敏捷的一些概念
[engine development] rendering architecture and advanced graphics programming
Déformation de la chaîne bm83 de niuke (conversion de cas, inversion de chaîne, remplacement de chaîne)
Detailed explanation of four modes of distributed transaction (Seata)
How to query the baby category of tmall on Taobao
Class part2
Talking about part of data storage in C language
分布式事务(Seata) 四大模式详解
retrofit
Analysis of gene family characteristics - chromosome location analysis
How does vs+qt set the software version copyright, obtain the software version and display the version number?
Puzzle (016.4) domino effect
NOI OPENJUDGE 1.5(23)
[ue4] geometry drawing pipeline
QT program font becomes larger on computers with different resolutions, overflowing controls
Get permissions dynamically
Open under vs2019 UI file QT designer flash back problem
C language to realize mine sweeping
2021-10-16 initial programming
PS tips - draw green earth with a brush