当前位置:网站首页>C language learning log 1.22
C language learning log 1.22
2022-06-13 04:57:00 【Today is also a day without baldness】
structure :
Statement :struct Structure name { data type Variable name ; data type Variable name ; data type Variable name ;};
It is the same as a local variable , If in main Function , Then only in main The function uses , If in main Declaration out of function , It can be regarded as a global variable .
Use and definition : for instance , If declared struct point{int year; int month; int day;}; , When using it, you only need to define variables for it in the , for example : struct point today; It means to define a today The structural variable of , And if you want to use today The value in the structure variable , Form the following : today.year; Is the structure variable name +“.” Operator .
Structural operations : 1. The assignment operation : Structural variables should also be assigned initial values , Sure struct Structure name Variable name ={0}; Is to assign all the values in the structure to 0.
Another form is to assign only some variables in the structure , The other variables, like the array, are assigned a value of... By default 0.
Assignment between two structural variables , There are structure variable names 1=(struct Structure name ){ value 1, value 2} It means Set the structure variable 1 The two values of are assigned to the two values in the structure variable .
There's another one Direct variable name 1= Variable name 2, The procedure simply adds the variable name 2 All values in are assigned to variable names 1.
2. Address fetch : It's different from arrays , When using the pointer , need & Operator to define a pointer , for example :struct Structure name * Pointer name = & Variable name , It means to define the address of a structure variable that points to a structure .
Structure as a function parameter : The whole structure can be passed into the function as the value of the parameter , Procedure is equivalent to creating a new structure variable in the function , And assign the value of the caller's structure , It can return a value , You can also return a structure .
How to use scanf The value of the passed in structure :1. Construct a structure function , The value passed in to the function is void, The middle creates an intermediate structure , Outgoing this structure , The structure of the main function calls this function .
struct point inputPoint()
{
struct point temp;
scanf("%d %d",&temp.x,&temp.y};
return temp;
}
void main()
{
struct point y={0,0};
y= inputPoint();
}
2. Use structural pointers : Construct a pointer to the structure , Pass this structure pointer parameter into the function , Finally, return to this structure . Here's a little knowledge : It can be used ‘->’ Represents the member of the structure variable the pointer refers to .
#include<stdio.h>
struct point
{
int x;
int y;
};
struct point*inputPoint(struct point*p)
{
scanf("%d",&p->x);
scanf("%d",&p->y);
return p;
}
int main(void)
{
struct point y={0,0};
inputPoint(&y);
printf("%d %d",y.x,y.y);
}
The nesting of structures :
An array of structures in a structure :
#include <stdio.h>
struct point{
int x;
int y;
};
struct rectangle {
struct point p1;
struct point p2;
};
void printRect(struct rectangle r)
{
printf("<%d, %d> to <%d, %d>\n", r.p1.x, r.p1.y, r.p2.x, r.p2.y);
}
int main(int argc, char const *argv[])
{
int i;
struct rectangle rects[ ] = {
{
{1, 2}, {3, 4}}, {
{5, 6}, {7, 8}}}; // 2 rectangles
for(i=0;i<2;i++) printRect(rects[i]);
}
边栏推荐
- Sub paragraph of Chapter 16
- Normal distribution (Gaussian distribution)
- E - Lucky Numbers
- RMQ、LCA
- String()和toString()方法得区别
- Colab tutorial (super detailed version) and colab pro/pro+ evaluation
- Test question bank and online simulation test for special operation certificate of construction scaffolder (special type of construction work) in 2022
- C # get all callable methods of WebService interface [webmethod]
- OpenCV中的saturate操作(饱和操作)究竟是怎么回事
- Chapter 17 free space management
猜你喜欢
Stepping on a horse (one stroke)
Conception d'un système basé sur MVC avec javaswing JDBC
Robot pose description and coordinate transformation
RMQ、LCA
[LeetCode]-二分查找
Brick story
Section 7 - structures
Mysql8.0.13 installation tutorial (with pictures)
Shell built-in string substitution
QT realizes message sending and file transmission between client and server
随机推荐
Keil uses j-link to burn the code, and an error occurs: Flash download failed - one of the "Cortex-M3" solutions
Chapter 14 introduction: memory operation API
QT brushes and brushes
Hidden implementation and decoupling, knowing Pimpl mode
Use service worker to preferentially request resources - continuous update
Nodejs parsing get request URL string
2021tami/ image processing: exploiting deep generative priority for versatile image restoration and manipulation
Analysis on the similarities and differences of MVC, MVP and mvvc
RuoYi-Cloud启动教程(手把手图文)
Chinese trumpet creeper
Sub paragraph of Chapter 16
UNO
Leetcode game 297 (20220612)
Common skills in embedded programming
Draw a hammer
利用Javeswingjdbc基于mvc设计系统
QT interface rendering style
The differences between the four startup modes of activity and the applicable scenarios and the setting methods of the two startup modes
Infinite cycle scrolling code Alibaba international station store decoration code base map scrolling black translucent display effect custom content decoration code full screen display
C#获取WebService接口的所有可调用方法[WebMethod]