当前位置:网站首页>N wars of C language -- common body and enumeration (VIII)

N wars of C language -- common body and enumeration (VIII)

2022-07-23 10:50:00 Ajie sends code online

The embedded The way , The most important thing is daily drip

                                                                --- Ajie sent the code online

Catalog

One 、 Shared body

Declaration and access of community  

How to understand “ All members of the community occupy the same section of memory ” 

The size of the temporary space of the community

purpose :

Two 、 enumeration  

3、 ... and 、typedef keyword  


One 、 Shared body

Shared body Is a special data type , Allow you to Store different data types in the same memory location . You can define a common body with multiple members , But only one member can have a value at any time . Commons provide an efficient way to use the same memory location .----- Novice tutorial  

The difference between structure and Commons is :

  • Each member of the structure takes up different memory , There is no influence on each other ;
  • and All members of the community occupy the same section of memory , Modifying one member affects all other members , The specific understanding will be at the end of the article ( How to understand “ All members of the community occupy the same section of memory ”) Explain in detail . 
  • Structural elements do not affect each other
  • Community assignment will result in overwriting  

Declaration and access of community  

First, let's talk about how the community is declared and used , In fact, the definitions and access methods of the common body and the structure are the same , Access members of the community , We use the member access operator (.)

Such as , Define a name Test Common body of  

union Test {
    int a;
    double pi;
    char str[20];
};
  • Define a Common body variable , Like a structure, you can declare it directly at the time of definition , Such as declaring a common body variable union_a,union_b 
union Test {
    int a;
    double pi;
    char str[20];
}union_a,union_b;
  • You can also define the common body type Test after , Directly define a common body variable , Such as  
union Test a,b

How to understand “ All members of the community occupy the same section of memory ” 

#include "stdio.h"

struct TestT
{
	int idata;
	char cdata;
	int ddata;
};

union TestU
{
	int idata;
	char cdata;
	int ddata;
};

int main()
{
	struct TestT t1;
	union  TestU u1;
	
	printf(" Structure t1 Its size is :%d\n",sizeof(t1));
	printf(" Consortium u1 Its size is :%d\n",sizeof(u1));
	
	printf(" Structure t1idata:%p\n",&t1.idata);
	printf(" Structure t1cdata:%p\n",&t1.cdata);
	printf(" Structure t1ddata:%p\n",&t1.ddata);
	
	printf(" Consortium u1idata:%p\n",&u1.idata);
	printf(" Consortium u1cdata:%p\n",&u1.cdata);
	printf(" Consortium u1ddata:%p\n",&u1.ddata);
	
	return 0;
}

Structure t1 Its size is :12
Consortium u1 Its size is :4
Structure t1idata:000000000061FE14
Structure t1cdata:000000000061FE18
Structure t1ddata:000000000061FE1C
Consortium u1idata:000000000061FE10
Consortium u1cdata:000000000061FE10
Consortium u1ddata:000000000061FE10

The size of the temporary space of the community

The temporary space of the community is generally slightly larger than that of the members with the largest temporary space in the community , A little tongue twister , Like the following example , Shared body Test in , member a Temporary use 4 Bytes , member pi Temporary use 8 Bytes , And the members str Temporary use 20 Bytes , Therefore, the temporary space of the community should be slightly larger than that of the members str Temporary space , Finally, through the program results, we can see that the temporary space is 24 Bytes . How much space should be used temporarily for the community , At present, the author has no need to have an in-depth understanding of him , Friends who need to know can consult other materials .

#include <stdio.h>

union Test {
    int a;
    double pi;
    char str[20];
};

int main()
{
    union  Test test;
    printf("sizeof test = %d\n", sizeof(Test));
    return 0;
}

Output results :sizeof test = 24 

purpose :

In data processing , Different uses need to be arranged for the same space , It is more convenient to use the community .

    example : There are data for several people , There are students and teachers . The student data includes : full name 、 number 、 Gender 、 occupation 、 class . Teacher data include : full name 、 number 、 Gender 、 occupation 、 position . It is required to use the same form to handle .

    It can be seen that : The data items of students and teachers are mostly the same , There is only one difference , Students' class , Teacher's position .

#include "stdio.h"

struct Person
{
	char name[32];
	int age;
	char zhiye;
	char addr[32];
	union{
		int class;
		char kemu[12];
	}mes;
};

int main()
{
	struct Person p[2];
	int i;
	
	for(i=0;i<2;i++)
	{
		printf(" Please enter occupation :t On behalf of the teacher ,s On behalf of students \n");
		scanf("%c",&(p[i].zhiye));
		
		if(p[i].zhiye == 's')
		{
			printf(" Please enter the student class :\n");
			scanf("%d",&(p[i].mes.class));
			printf(" Please enter the student's name :\n");
			scanf("%s",p[i].name);
		}
		else
		{
			printf(" Please enter the teacher's subject :\n");
			scanf("%s",p[i].mes.kemu);
			printf(" Please enter the teacher's name :\n");
			scanf("%s",p[i].name);
		}
		getchar();
	}
	
	for(i=0;i<2;i++)
	{
		if(p[i].zhiye == 's'){
			printf(" The student's name is :%s, The class is %d\n",p[i].name,p[i].mes.class);
		}
		else{
			printf(" The teacher's name is :%s, Occupation is %s\n",p[i].name,p[i].mes.kemu);
		}
	}
	
	return 0;
}

Please enter occupation :t On behalf of the teacher ,s On behalf of students
t
Please enter the teacher's subject :
Chinese language and literature
Please enter the teacher's name :
Zhou
Please enter occupation :t On behalf of the teacher ,s On behalf of students
s
Please enter the student class :
3
Please enter the student's name :
Li
The teacher's name is : Zhou , My profession is Chinese
The student's name is : Li , The class is 3 

Two 、 enumeration  

  • The enumeration is C A basic data type in language , It can make data concise and readable
  • The definition format is :
enum  Enum name { Elements 1, Elements 2};
  • Why use enumeration ?
#define MON  1
#define TUE  2
#define WED  3
#define THU  4
#define FRI  5
#define SAT  6
#define SUN  7

For example, the above codes , It looks very complicated , If you use enumeration The way :

enum DAY{MON=1,TUE,WED, THU, FRI, SAT, SUN};

Be careful : The default value of the first enumeration member is integer 0, The value of the subsequent enumeration member is added to the previous member 1. In this example, we define the value of the first enumeration member as 1, The second is 2, And so on  

enum nums{a,b,c,d=5,e};

There is no enumeration element with specified value , Its value is the previous element plus 1 

  • Definition of enumeration variables
enum DAY{MON=1,TUE,WED,THU,FRI};
enum DAY day;
  • example
#include <stdio.h>

enum DAY
{
     	 MON=1, TUE, WED, THU, FRI, SAT, SUN
};

int main()
{
    enum DAY day;
    day = WED;
    printf("%d",day);
    return 0;
}

Running results :

  • The application of enumeration in examples
#include <stdio.h>
#include <stdlib.h>

int main(){
    enum color { red=1, green, blue };
    enum  color favorite_color;
    printf(" Please input your favorite color : (1. red, 2. green, 3. blue): ");
    scanf("%u", &favorite_color);
    
	switch (favorite_color)
	{
		case red:
			printf(" Your favorite color is red ");
			break;
		case green:
			printf(" Your favorite color is green ");
			break;
		case blue:
			printf(" Your favorite color is blue ");
			break;
		default:
			printf(" You didn't choose the color you like ");
    }
    return 0;
}

Please input your favorite color : (1. red, 2. green, 3. blue): 1
Your favorite color is red  

  • Traversing enumeration types
#include <stdio.h>

enum DAY{
      MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;

int main()
{
    //  Traversal enumeration elements 
    for (day = MON; day <= SUN; day++) {
        printf(" Enumeration elements :%d \n", day);
    }
	
	return 0;
}

Running results :

Enumeration elements :1
Enumeration elements :2
Enumeration elements :3
Enumeration elements :4
Enumeration elements :5
Enumeration elements :6
Enumeration elements :7 

If it looks like the following Enumeration type Discontinuous , You cannot traverse  

enum{
    day1,
    day2 = 10,
    day3
};
#include<stdio.h>

enum DAY{MON=1, TUE, WED, THU=6, FRI, SAT, SUN}day;

int main()
{
    for(day=MON;day<=SUN;day++)
	{
        printf("%d\t",day);
    }
    
	return 0;
}

1       2       3       4       5       6       7       8       9 

3、 ... and 、typedef keyword  

Name the existing variable type  

#include "stdio.h"

typedef unsigned int u8;
typedef int u16;
typedef int arr[10];

struct Test
{
	int data;
	int data2;
};

typedef struct Test T;

typedef struct
{
	int data1;
	int data;
}Demo;

void printInfo(T t)
{
	printf("%d\n",t.data);
}

int main()
{
	arr a;
	a[0] = 10;
	printf("%d\n",a[0]);
	
	struct Test t1;
	t1.data = 100;
	printf("%d ",t1.data);
	
	T t2;
	t2.data = 1000;
	printInfo(t2);
	
	Demo d;
	d.data1 = 999;
	printf("%d ",d.data1);
	
	return 0;
}

10
100 1000
999 

#include "stdio.h"

typedef struct
{
	int num;
	char name[32];
	char sex;
}Person,*pPerson;

void printInfo(Person p)
{
	printf("%d Sister No :%s %c\n",p.num,p.name,p.sex);
}

void printInfo2(Person *p)
{
	printf("%d Sister No :%s %c\n",p->num,p->name,p->sex);
}

void printInfo3(pPerson pp)
{
	printf("%d Sister No :%s %c\n",pp->num,pp->name,pp->sex);
}

int main()
{
	Person p1 = {1," lily ",'g'};
	Person p2 = {2," Beautiful ",'g'};
	printInfo(p1);
	printInfo(p2);
	
	pPerson pp1 = &p1;
	pPerson pp2 = &p2;
	printInfo2(pp1);
	printInfo2(pp2);
	
	Person *pp3 = &p1;
	Person *pp4 = &p2;
	printInfo3(pp3);
	printInfo3(pp4);
	
	return 0;
}

1 Sister No : lily g
2 Sister No : Beautiful g
1 Sister No : lily g
2 Sister No : Beautiful g
1 Sister No : lily g
2 Sister No : Beautiful g  

原网站

版权声明
本文为[Ajie sends code online]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230451162455.html