Struct, common, enum

Keywords: Linux Ubuntu

1. structure

**
Define a person data type

struct person{

 char name[20];
 int height;
};

//The above indicates that you have defined a structure data type, whose name is struct person.


typedef struct person per;  //Give struct person an alias called per


typedef struct person{
	char name[20];
	int height;
}per;
 


//Define a structure variable
//Type name variable name;
per  p1={"zhangsan",175};
//Structure variable access member variable name. Member name
scanf("%s%d",p1.name,&p1.height);
printf("%s%d\n",p1.name,p1.height);

//Structure variable assignment 
per p2;  p2=p1;
   //Define a structure pointer variable
per * pper=NULL;
pper=&p1; 
//Structure pointer access member pointer name - > member name	
scanf("%s%d",pper->name,&pper->height);
printf("%s%d",pper->name,pper->height);

  **

2. Byte alignment

**
Byte alignment: the system allocates several bytes at a time

Natural alignment:

char ch;  1
short s:  2
int i;    4
float f;  4
double d; 4 byte alignment (8)  gcc

//Structure alignment: according to the largest byte alignment among members
#include<stdio.h>

struct A{
	short s;
	int b;
	char c;
};
struct B{
	int b;
	short s;
	char c;
};

int main(void)
{
	printf("%d\n",sizeof(struct A));12
	printf("%d\n",sizeof(struct B)); 8
}

**

3. shared body:

**

Create a new data type by yourself
union un{
	int a;
	char c;
};
//Define a common data type, whose name is union un;
//The first address of all members of the common body is the same

sizeof(union un) ========= the most memory occupied by all members



   **

4. Small end sequence

**
The place with low byte and low address is called small endian
Low byte with high address is called big endian
How to determine whether a pc is a large or small end sequence
#include<stdio.h>
union un{
int a;
char c;
};
//The name of a common data is union un.
int main(void)
{
//Define a common variable
union un u1;
u1.a=0x12345678;

if(u1.c==0x78)
{
	printf("The pc Small end order\n");
}else{
	printf("The pc For large ends\n");
}

return 0;
}

**

5. enumeration

**
Enumeration defines constants in another way:

(1).Macro definition  #define N 5
(2).const 
int a=5;
a=8;

const int b=10;
//b=20;

char ch='A';
   //const char * p=&ch;  
char const * p=&ch;  
  //	*p='B '; immutable
char sh='B';
p=&sh;


char * const ps=&ch;
ps=&sh; //ps immutable


(3).Enumeration can make your code more semantic
  
  ERR  //ERR returned if insert failed
  OK   //Insert successfully and return to OK
#define  ERR -1
#define  OK 0
#define  HEAD -1
#define  TAIL 0
 enum opt
{
	ERR=-1,OKļ¼ŒHEAD=-1,TAIL
};
ERR The default is 0, if you want it to start from-1 Start, you need to change

**

6. macro

**
#define N 5
Macro with parameters
(1) macro with one parameter is a replacement mode:
The following macro definitions are set:
#define Y(n) ((N+1)n)
Rigorous writing: define y (n) ((n + 1) (n))
Execute the statement z=2*(N+Y(5+1)); after that, the value of Z is.
A. error B, 42 C, 48 D, 54

#include<stdio.h>
#define  MIN(x,y)  (x)<(y)?(x):(y)
void	main()
{
int  i=10, j=15,k;
 k=10*MIN(i,j);   15
printf("%d", k);
}

//Write a standard macro to achieve the minimum value between two numbers
#define MIN(x,y)  ((x)<(y)?(x):(y))

**

7.main function:

**

 System passed to main

  #include<stdio.h>
  int main(int argc, const char *argv[])
 {
//argc indicates the number of parameters passed
//argv passes in an array, each element of which is a char*
//Summary, argc char s*
int i=0;

for(i=0;i<argc;i++)
{
	printf("argv[%d]=%s\n",i,argv[i]);
}

return 0;

}

   ******Terminal:******linux@ubuntu:/mnt/hgfs/19071/7-19$ ./main zhangsan lisi wangwu
argv[0]=./main
argv[1]=zhangsan
argv[2]=lisi
argv[3]=wangwu

Posted by andrin on Thu, 17 Oct 2019 14:44:48 -0700