当前位置:网站首页>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]);
}

原网站

版权声明
本文为[Today is also a day without baldness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280517425776.html