structure

Original link:
http://www.dongeasy.com/software-development/embedded-system/1710.html

Basic definition: Structure, which is like packaging and encapsulation in general, encapsulates some variables with common characteristics (such as attributes belonging to a certain type of things, often an aggregation of certain business-related attributes), and accesses them through certain methods. Modify internal variables.

Structure definition:

The first: only the structure definition

struct stuff{
   char job[20];
   int age;
   float height;
};

The second: attach the initialized structure definition of the "structure variable" of the structure type

struct stuff{
   char job[20];
   int age;
   float height;
}DongEasy;

Maybe it is easy to be confused if you are not used to it at first, but in fact, this is equivalent to:

struct stuff{
   char job[20];
   int age;
   float height;
};
struct stuff DongEasy;

The third type: If you only use a variable DongEasy for the structure, you no longer need to use

struct stuff yourname;

to define the second variable.

Then, the structure definition of additional variable initialization can be further simplified into the third one:

struct{
   char job[20];
   int age;
   float height;
}DongEasy;

Remove the structure name, which is more concise, but cannot define other variables of the same structure

Definition and access of structure variables and their internal member variables:

As mentioned in the second paragraph just now, the declaration of structure variables can be used:

struct stuff yourname;

The definition of its member variables can follow the declaration:

struct stuff DongEasy = {"manager",30,185};

Also consider assignments between structs:

struct stuff faker = DongEasy; //或    struct stuff faker2; //      faker2 = faker;

Structure member variables can be accessed with "->" in addition to the symbol "."

DongEasy.job[0 = 'M';
DongEasy.job[1 = 'a';
DongEasy.age = 27;
DongEasy.height = 185;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325701011&siteId=291194637