当前位置:网站首页>C language structure - learning 27
C language structure - learning 27
2022-06-12 00:39:00 【XG. Solitary dream】
This paper is finally updated at 2022 year 02 month 22 Japan , Has more than 6 I didn't update it . If the article content or picture resources fail , Please leave a message for feedback , I will deal with it in time , thank you !
Type of structure
- User created by Different types of data Composed of a combined data structure , It's called Structure .
- The general form of declaring a struct type is :
structStructure name{ Member list };
explain :
- 1. Many types of structures can be designed
- for example :
struct Teacherstruct Workerstruct DateAnd so on , Each contains different members
- 2. Members can belong to another structure type
- for example :
struct Date
{ int month; int day; int year; };
struct Student{
int num;
char name[20];
char sex;
int age;
struct Date birthday;
};Structure type variable
- 1. First Declare the struct type , Again Define variables of this type .
- for example :
// Declare the struct type
struct Student{
int num;
char name[20];
char sex;
int age;
};
// Define variables of this type
// Structure type name Structure variable name
struct Student student1,student2;- 2. Define variables while declaring types
- for example :
struct Student{
int num;
char name[20];
char sex;
int age;
}student1,student2;- 3. Define struct type variables directly without specifying type names
- The general form is :
struct{ List of members } Variable name table column ;Specified a unknown The type of structure .
- The general form is :
- for example :
struct
{
int num;
char name[20];
char sex;
int age;
}student1,student2;explain :
- 1. Type of structure And Structural variable It's a different concept , Don't mix up .
- Only variables can be assigned 、 Access or operation , Instead of assigning a value to a type 、 Access or operation .
- At compile time , Yes The type is unallocated space Of , only Allocate space to variables . Memory allocated to structural variables Storage space yes The total storage space occupied by each member variable .
- 2. In structure type The member name can be the same as the variable name in the program , but They don't represent the same object .
- 3. Structure variables can be initialized and assigned at the time of definition . When initializing a structure variable , The initial values assigned are placed in a pair of curly brackets in sequence .
- for example :
struct Student{
int num;
char name[20];
char sex;
int age;
}student1={2022160123,"Zhang san"," male ",24},student2;A reference to a member of a structure variable
- Structure variables are construction type variables , There are two objects that can access operations .
- 1. Structure Members of variables ;
- 2. Structure Variable itself , Represents the All data members .
- Refer to members of structural variables
- Access form of structure variable members :
Structure variable name.Member name
- for example :
student1.name
- Order number “.” be called member operator , yes Binary operator , Priority is first stage , Associativity is from left to right .
- Access form of structure variable members :
- Members of structure variables can participate in various operations like ordinary variables
- for example :
student2.num = student1.num++;
- for example :
explain :
- 1. If the member itself is a structure type , You need several member operators , Step by step Only the lowest level members can use .
- for example :
student2.birthday.day =18;
- for example :
- 2. You can refer to the address of the variable member of the structure , here “&” Put it on The structure variable name is preceded by .
- for example :
scanf( "%d" ,&student2.num);
- for example :
- 3.“&” and “.” At the same time , Operator “.” High priority
- for example :
- expression
student1.num++Equivalent to(student1.num)++.
- expression
- for example :
- 4. Same structure type The structure variable of can Direct mutual assignment .
- for example :
- Put the variable student1 All member values of are assigned to variables one by one student2 All member values of
student2 = student1;
- for example :
- 5. Structure variables are different from basic type variables , Don't use it directly for arithmetic 、 Operations such as relation and logic , You can only compare members one by one .
- 6. You cannot input structural variables as a whole / Output , You can only input or output one by one for each member .
- 7. A piece of contiguous memory occupied by structural variables The first address Call it The address of the structure variable , The number of memory units occupied by each member The first address be called Member's address , Both addresses can refer to .
Example
Put a student's information ( Including student ID 、 full name 、 Gender 、 Age ) Put it in a structure variable , Then output the student's information .
#include <stdio.h>
void main() {
struct Student {
long int num;
char name[20];
char sex;
int age;
}std = { 10001,"Zhang san",'M',20 };
printf(" Student number :NO.%ld\n full name :%s\n Gender :%c\n Age :%d\n",
std.num,std.name,std.sex,std.age);
}Enter the student number of two students 、 Name and score , Output the student number of students with higher grades 、 Name and score
#include <stdio.h>
void main() {
struct Student {
long int num;
char name[20];
float score;
}std1,std2;
printf(" Please enter student information :\n");
// For array input ,scanf_s To define the buffer size , and name Don't add... In front &,name Itself is the address
scanf_s("%d%s%f", &std1.num, std1.name,21, &std1.score);
scanf_s("%d%s%f", &std2.num, std2.name,21, &std2.score);
if (std1.score > std2.score) {
printf(" Student number :NO.%ld\n full name :%s\n achievement :%.2f\n",
std1.num, std1.name, std1.score);
}
else if (std1.score < std2.score) {
printf(" Student number :NO.%ld\n full name :%s\n achievement :%.2f\n",
std2.num, std2.name, std2.score);
}
else {
printf(" Student number :NO.%ld\n full name :%s\n achievement :%.2f\n",
std1.num, std1.name, std1.score);
printf(" Student number :NO.%ld\n full name :%s\n achievement :%.2f\n",
std2.num, std2.name, std2.score);
}
}边栏推荐
- 2022 Tibet's latest eight members (security officer) simulated test question bank and answers
- APP项目怎么测?如何搭建测试体系
- 苹果手机wps如何改字体大小
- Investment analysis and prospect Trend Research Report of global and Chinese cyclopentanyl chloride industry 2022-2028
- How to optimize the opening speed of website pages to improve the experience?
- "Failure" of the prospectus of Laowang: Laowang made its first dash for listing in Hong Kong, and the turnover rate continued to decline
- [node] common methods of path module
- repeat_ L2-009 red envelope grabbing_ sort
- LabVIEW Arduino电子称重系统(项目篇—1)
- Big sword
猜你喜欢

What is bonded warehouse and what is the difference between them

Unified certification center oauth2 high availability pit

Devops landing practice drip and pit stepping records - (1)

What are the advantages of Tiktok applet

功能测试如何1个月快速进阶自动化测试?明确这2步就问题不大了

win10系统pscs6如何卸载

Enterprise wechat H5_ Integrated message decryption class, message push get and post callback processing

干货|一次完整的性能测试,测试人员需要做什么?

Tencent programmer roast: 1kW real estate +1kw stock +300w cash, ready to retire at the age of 35

Jiaming's day13 of C learning -- structure and structure pointer
随机推荐
一、Flutter 入门学习写一简单客户端
2023 spring recruit | ant group middleware Intern Recruitment
环境搭建2
UVM: transaction level modeling (TLM) 1.0 communication standard
JS -- prevent automatic recovery of page position
2022 Tibet latest special worker (construction elevator) simulation test question bank and answers
How much does it cost to develop s2b2c mall system
Lambda中间操作flatMap
DDD exaggeration, Eric Evans made a bad start
Jiaming's day13 of C learning -- structure and structure pointer
What does the Red Cross of win10 folder status indicate
How to uninstall pscs6 in win10 system
MySQL basic tutorial -- MySQL transaction and storage engine
The "hard words" about interface testing
Lambda创建流
Sword finger offer 09 Implementing queues with two stacks
Tencent programmer roast: 1kW real estate +1kw stock +300w cash, ready to retire at the age of 35
730.Count Different Palindromic Subsequences
ironSource&nbsp; New functions are released, and developers can adjust advertising strategies in real time in the same session
Characteristics of JS logical operators