当前位置:网站首页>Understanding of structure in C language

Understanding of structure in C language

2022-06-24 03:55:00 Good pie notes

1, What is the structure

A structure is a type of composite data , Its member type can be an interrelated basic data type or a structure type

2, Use occasion

When dealing with a large number of different data types that are related , For example, a student's information , Student number (int), full name (char *), Age (int) Etc , You can use the structure , Greatly improved efficiency

3, How to use it?

(1) Declare or define the structure type

Different from the definition of basic data variables , Because the system already knows the structure of basic data types , Type name Variable name

You can do it , But the structure is defined by the user ,

The first step is to declare or define the structure type , Tell the system that there is this type , What is it like , The way to define the structure type is : Use keywords struct Followed by type name

struct Type name

{

Member list ;// A member class table is a member type name ; A type can be a basic data type , It can also be a structure type

}

Such as :struct Student

{

int num;

char name[20];

char sex;

int age;

char address[20];

}; // This defines a structure type Student, It contains all kinds of information about students

(2) Define structure variables

When declaring a structure type ( It's like a template ) After that, you can define structure variables , There are three main ways :

1> > Declaring structure types is separate from defining structure variables ( The most common and flexible way )

struct Student

{

int num;

char name[20];

char sex;

int age;

char address[20];

};// After declaring the structure type struct Student after , Define structure variables

struct Student studnet1,student2;// Two structure variables are defined student1 and student2

2>> Define structure variables while declaring structure types . Such as :

struct Student

{

int num;

char name[20];

char sex;

int age;

char address[20];

}studnet1,student2;

3>> Define structure variables while declaring structure types ( Anonymous object ), But there is no type name Such as :

struct

{

int num;

char name[20];

char sex;

int age;

char address[20];

}studnet3;

4, Use caution

1>> Structure type and structure variable are two different concepts , The structure type is equivalent to a template , At compile time

Not allocating storage space , Only for defined .

Structure variables allocate corresponding storage space according to the definition of the template , In this case, only structural variables can be assigned , save

Summation operation , The structure type cannot be assigned , Access and operation

2>> Structure type and structure variable cannot be defined repeatedly in the same scope , Except for anonymous type objects

3>> It can be inside or outside the function , The effect and usage are similar to local variables and global variables , You can also nest definitions

4>> You can define the same struct type inside and outside the function , Also follow the principle of proximity

5>> Only when the structure variable is defined can it be initialized in batch , After defining the structure variable , Only one by one

Initiation

5, The difference with array

1>> Structures can hold different types of elements , Arrays can only be of the same type

2>> The structure type needs to be defined by us . Array is to add... With other types [ Element number ]

3>> Structure memory allocation is very special , Use alignment principles , It doesn't have to be the number of bytes and , And the array must be the

There are the number of bytes of the element and .

4>> A pointer to a structure can refer to a name -> Structure element name ( Take the element ); Not an array

6, Array of structs

Structure array is essentially an array , Array elements are structural variables of the same type , For example, define a Student Structure

Array is used to store the information of all students in a class

7, Structure pointer

Is a pointer to a structure variable , Usage is as follows

struct Person

{

char *name;

int age;

}; // Define the structure type

struct Person per={"wangjintaof",2};// Define structure variables and initialize them in batches at the same time

struct Person *p=&per; // Define a pointer to a structure type , And initialization , Make it point to pe

Then you can use the pointer to reference and access the elements of the structure variable , The three methods are as follows

1>>>. P->name = "wangjintao";// to name Member variable assignment

2>>>. (*p).name = "wangjitnao";// to name Member variable assignment 、

3>>>. Per.name="wangjitnao";

原网站

版权声明
本文为[Good pie notes]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/09/20210917190323830i.html