The role of C-language shared body

Source: Internet
Author: User

First, Introduction

1. The common body (Union), which is defined in the following format:

Union Common body Name {
Member List
};

A common body is also sometimes called a union or a union, which is also the meaning of the word Union.

2. The difference between a struct and a common body is that each member of the struct occupies a different memory and has no effect on each other, while all members of the common body occupy the same memory, and modifying one member affects all remaining members.

The structure consumes more memory than equals the sum of the memory occupied by all members (there may be gaps between the members), and the memory occupied by the common body equals the memory occupied by the longest member. The shared body uses a memory overlay technique that only holds the value of one member at a time, and if the new member is assigned a value, the value of the original member is overwritten. In fact, a piece of memory is divided into several categories according to different data types. May be a group of type int, one set when double type.

Second, the role (single-chip computer aspect)

The common body is used less in the general programming, and it is used more in the monolithic microcomputer. For a PC, one example that is often used is: a table of student information and teacher information is available. Student information includes name, number, gender, occupation, score, teacher's information including name, number, gender, occupation, teaching subjects. Take a look at the following table:

Name Num Sex Profession Score/course
Hanxiaoxiao 501 F S 89.5
Yanweimin 1011 M T Math
Liuzhentao 109 F T 中文版
Zhaofeiyan 982 M S 95.0

F and M represent females and males respectively, s means students, T denotes teachers. As you can see, the data that students and teachers contain are different. It is now required to put this information in the same table and design the program to enter personnel information and then output.

If everyone's information is treated as a struct variable, then the first 4 member variables of the teacher and the student are the same, and the 5th member variable may be score or course. When the value of the 4th member variable is s, the 5th member variable is score, and when the value of the 4th member variable is T, the 5th member variable is course.

Through the above analysis, we can design a structure containing a common body, see the following code:

12345678910111213141516171819202122232425262728293031323334353637 #include <stdio.h>#include <stdlib.h>#define TOTAL 4 //人员总数struct{ char name[20]; int num; char sex; char profession; union{  float score;  char course[20]; } sc;} bodys[TOTAL];int main(){ int i; //输入人员信息 for(i=0; i<TOTAL; i++){  printf("Input info: ");  scanf("%s %d %c %c", bodys[i].name, &(bodys[i].num), &(bodys[i].sex), &(bodys[i].profession));  if(bodys[i].profession == ‘s‘){ //如果是学生   scanf("%f", &bodys[i].sc.score);  }else{ //如果是老师   scanf("%s", bodys[i].sc.course);  }  fflush(stdin); } //输出人员信息 printf("\nName\t\tNum\tSex\tProfession\tScore / Course\n"); for(i=0; i<TOTAL; i++){  if(bodys[i].profession == ‘s‘){ //如果是学生   printf("%s\t%d\t%c\t%c\t\t%f\n", bodys[i].name, bodys[i].num, bodys[i].sex, bodys[i].profession, bodys[i].sc.score);  }else{ //如果是老师   printf("%s\t%d\t%c\t%c\t\t%s\n", bodys[i].name, bodys[i].num, bodys[i].sex, bodys[i].profession, bodys[i].sc.course);  } } return 0;}

Operation Result:

Input Info:hanxiaoxiao 501 F S 89.5
Input info:yanweimin 1011 m T Math
Input Info:liuzhentao 109 F t 中文版
Input Info:zhaofeiyan 982 M S 95.0

Name Num Sex Profession Score/course
Hanxiaoxiao 501 F S 89.500000
Yanweimin 1011 m T Math
Liuzhentao 109 F t 中文版
Zhaofeiyan 982 M S 95.000000

Third, case (embedded)

The following code uses the overlay technique to receive a uint array of data, which is then combined into floating-point (32-bit) data for amplitude.

Staticunion{uint8_t data[ -]; floatactval[6];}         posture; if(Usart_getitstatus (USART1, usart_it_rxne) = =SET)    {usart_clearitpendingbit (USART1,USART_IT_RXNE); CH=Usart_receivedata (USART1); Posture.data[i]=ch; I++; if(I >= -) {i=0;  Break; }} Setpositionx (posture. actval[3]); Setpositiony (posture. actval[4]); Setangle (posture. actval[0]); 

The role of C-language shared body

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.