Recognize struct, struct

Source: Internet
Author: User

Recognize struct, struct

 

ST encapsulates the register group in the form of struct. Therefore, it is also an important step to learn the Firmware Library.

In C, a struct is a data structure that can be declared as a variable, pointer, or array to implement a complex data structure. In MDK, struct and struct pointers are used in many places.

Struct is defined as follows:

Struct name {member list:} variable name list;

Struct type name = keyword struct + struct name. struct name is specified by the user, which is different from other struct types.

The following struct:

Struct date // declare a struct type
{
Int month;
Int day;
Int year;
};
Struce student // declare a struct type
{
Int num;
Int name [20];
Int sex;
Int age;
Struct date birthday; // brithday is of the struct date type
};

This only specifies a struct type, which is equivalent to a model with no specific data, and the system does not allocate actual memory units to it. In order to use struct data in the program, you need to define struct type variables and put data in the struct. You can define the struct type variable using the method in step 3:

1. Declare the struct type and then define the variable.

struct student student1,student2;

 

A struct student has been defined above and can be used to define variables. The definitions in this form are similar to those in "int a, B.

2. Define variables while declaring types

  struce student                 
{
int num;
int name[20];
int sex;
int age;
struct date birthday;
}student1,student2;
 

In this way, the declared type and definition variables are put together to directly view the structure of the struct. This method is suitable for writing small programs.

 

3. directly define struct type variables without specifying the type name

 struct {         int num;         int name[20];         int sex;         int age;         struct date birthday;              }student1,student2;

In this way, an unknown struct type is specified without the struct name. Therefore, variables cannot be defined for this struct type.

Struct variable reference:

After defining the struct variable, You need to reference this variable. There are several rules for referencing the variable:

1. Similar struct types can be assigned values to each other, for example:

                                     student1=student2;

2. Find the lowest-level members with several struct member operators at the first level, and only assign values or access operations to the lowest-level members, such:

                                    student1.num=10010;                                    student1.birthday.year=2001;

3. struct variable members can perform various operations like common variables, such:

                                   student1.score=student2.score;                                   sum=student1.score+student2.score;

Struct variable initialization example:

#include<stdio.h>void main(){  struct student       {           long int num;           char name[20];           char sex;           car addr[20];              }a={10001,"li",'M',"beijing"}  printf("No.:% ld\nname:% s\nsex:% c\naddress:% s\n",             a.num,a.name,a.sex,a.addr);}

The running result is as follows:

No.: 10001name: lisex: Maddress: beijing

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.