C language_0328 Notes_Structure: constant symbolization/int value/custom enumeration/automatic enumeration counting/not very commonly used

Table of contents

What is structure?

11.2.1 Structure type

The form of declaring a structure

method one:

 Method Two:

 Method three: (common)

Structure variables

 Structure variable initialization

structure member

structural operations

structure pointer


What is structure?

If we want to record a series of data of abstract objects, we don’t want to define them one by one. At this time, we can use the structure type of C language.

A structure is a composite type that can contain member variables of many different types.

11.2.1 Structure type

struct date {

int month;

int day;

int year;

  • //struct is a keyword. Inside the brackets are the members of the structure. 
  • // Every date in the future will have these three members
  • #include ‹stdio.h>
    int main(int arg, char const *argv(])
    {
    struct date {
    int month;
    int day;
    int year;
    };
    struct date today;
    //使用结构类型,建立了today结构变量
    today.month = 07;
    today. day = 31;
    today. year = 2014;
    printf ("Today's date is %i-i-%i.\n"
    today. year, today.month, today. day|
    return 0;
    }
  • Like local variables, a structure type declared inside a function can only be used inside the function, but if declared externally, it can be used by multiple functions. So usually put the structure declaration part outside all functions
  • Similar to the previous enumeration, you need to bring the struct keyword when using it

The form of declaring a structure

method one:

  1.  A structure named point is declared and two variables p1 and p2 are created.
  2. Two variables p1 and p2 are created. The types of p1 and p2 are struct point [p1 and p2 contain the values ​​of x and y]
  3. Next, you can use the p1 and p2 variables to do something
struct point{
int x;
int y;
};
struct point p1,p2;
 
//声明了一个结构名为point,创建了两个变量p1和p2

 Method Two:

  1. //Declare a structure without a name and create two variables p1 and p2
  2. //p1 and p2 are both nameless structures with x and y values
  3. //When you only need to use two variables at the moment, you can use this unnamed method
struct {
int x;
int y;
}p1,p2;
 
//声明了一个结构但没有名字,创建了两个变量p1和p2
//p1和p2都是无名结构,有x和y值
//当你只需要在当下使用两个变量,可以用这种不起名字的方式

 Method three: (common)

  1.  A structure named point is declared and two variables p1 and p2 are created.
  2. The variables of the structure are defined. p1 and p2 are both points, with x and y values ​​inside.
struct point{
int x;
int y;
}p1,p2;

Structure variables

Example:

struct date today;

today.month=06;

today.day=19;

today.year=2005;

  • So there are three int types in the today variable . The part declared outside the function is declaring a new type, while the part declared inside the function is defining variables of this type. After declaration, you can create multiple different variables according to the template, containing day, year, etc. inside.

 Structure variable initialization

After we create a structure variable, it is inside the function and is a local variable with no initial value. Because of the lessons learned from the past, if you don't assign an initial value, you will be given a messy memory value. So how to assign an initial value to it?

struct date{     int month;     int day;     int year;   }; int main() { struct date today = {07,31,2014}; //One is to assign values ​​in the order of declaration struct date thismonth = {.month= 7, .year=2014 };     //Similar to an array, no value is assigned to day, and 0 is automatically assigned }




 


   

   

structure member

  • Structure members are similar to array members , but array members must be of the same type, and structure members do not
  • Member access methods are different. Arrays are accessed through square brackets [] and subscripts (numbers in square brackets), while structures are accessed through dots (.) and names
  • Note that what appears on the left of [.] must be a structure variable (entity), and the structure type is virtual, just telling the compiler that there is such a type

    eg:a[0]=10;

    today.day\p1.x 

structural operations

  • To access the entire structure, use the name of the structure variable directly
  • The whole structure can be assigned, taken address, or passed to function parameters
  • The following two operations cannot be done with arrays
    • day=today;The result is the same;but! ! ! Arrays cannot be directly equal to two arrays
    • If the values ​​of day and today are changed again, they are different, indicating that these are two different and separate variables.
p1=(struct point){5,10};
//(struct point 是强制类型转换,将5,10强制转换为point结构变量)
//相当于p1.x=5, p1.y=10
 
p1=p2;
//相当于p1.x = p2.x, p1.y = p2.y

example:

#include<stdio.h>
 
struct date{
    int month;
    int day;
    int year;
};
 
int main()
{
    struct date today;
    today = (struct date){07,31,2014};
//给today赋值
    struct date day;
 
    day=today;//数组不能做相等!!!
//让day=today,分别打印
    printf("Today's date is %i-%i-%i.\n",
    today.year,today.month,today.day);
 
    printf("The day's date is %i-%i-%i.\n",
    today.year,today.month,today.day);
}

structure pointer

 The essence of an array is a pointer , but a structure variable is not, so its name cannot be used as an address, and the & operator needs to be used.

struct date *pDate = &today

 At this time, a pointer is created pointing to the today variable. 

(Develop a good habit and use & when taking addresses)

Guess you like

Origin blog.csdn.net/Shmily_as33/article/details/129819182