Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What does struct mean in C language? How to use it? Can you explain it in simpler terms?
What does struct mean in C language? How to use it? Can you explain it in simpler terms?

Structure (struct)

A structure is a combination of various variables composed of basic data types and named with an identifier.

Different data types can be used in structures.

Structure description and structure variable definition

In Turbo C, structure is also a data type, and structure variables can be used. Therefore, like other types of variables, when using structures Variables must be defined first. The general format for defining structure variables is:

struct structure name?

{

Type? Variable name;

Type? Variable name ;?

...

} Structure variable;

The structure name is the identifier of the structure, not the variable name. ? The types are the five data types described in Section 2 (integer, floating point, character, pointer and valueless).

Each type variable that constitutes a structure is called a structure member. It is like an element of an array, but elements in an array are accessed by subscripts, while members of a structure are accessed by variable names. Here is an example to illustrate how to define structure variables.

struct string

{

char name[8];

int age;?

char sex [2];

char depart[20];

float wage1, wage2, wage3, wage4, wage5;

} person;

< p>This example defines a structure variable person with a structure name of string. If the variable name person is omitted, it becomes a description of the structure. Structure variables can also be defined using the declared structure name. When defined in this way, the above example becomes:?

struct string

{

char name[8];

int age; < /p>

char sex[2];

char depart[20];

float wage1, wage2, wage3, wage4, wage5;

}; struct string person;?

If you need to define multiple structure variables with the same form, it is more convenient to use this method. It first makes a structure description, and then uses the structure name to define the variables. ?

If the structure name is omitted, it is called an unnamed structure. This situation often occurs inside a function. When using this structure, the previous example becomes:

struct

p>

{

char name[8];?

int age;

char sex[2];

char depart[20];

float wage1, wage2, wage3, wage4, wage5;?

} Tianyr, Liuqi;

Structure as a data type ,? Therefore, the defined structure variables or structure pointer variables also have local variables and global variables, depending on the location of the definition.

The structure variable name does not point to the address of the structure, which is different from the meaning of the array name. Therefore, if you need to require the first address of the first member in the structure, it should be &[structure variable name].

Extended information:

What operations can be performed on structure variables?

Structure variables cannot be added, subtracted, or multiplied or divided by each other, but structure variables Variables can be assigned to each other. That is, you can assign one structure variable to another structure variable. But the premise is that the structure types of the two structure variables must be the same.

The reference method of structure variables determines:

The "structure variable name" can have the same name as the "structure member name".

The "structure variable name" can have the same name as the "structure name".

"Members in structure variables defined by two structure types can have the same name."

For example, if a structure type is defined to store student information, which contains the member "char name[20];", then if a structure type is defined to store teacher information, Then there can also be members "char name[20];"

Because when the structure members are referenced, they must be referenced using the method of "structure variable name.member name", and you can just use the reference. Differentiate them so they don't conflict and therefore can have the same name!

You can have the same name as long as there is no conflict! However, the names of two structure variables cannot have the same name, because they cannot be distinguished and conflicts will occur. Of course, what we are talking about here is that within the same scope, if you define a local variable a in a function, then of course you can also define a local variable a in another function. They do not affect each other.