Notes after reading modern methods of C programming

The members of a structure are stored in memory in the order they are declared. The location of the structure part1 is the first location of the first member, and how much memory each member occupies depends on their data type.

struct {
int a;
char b[20];
}part1 ={528."zrr"}, part2 = {333."ZRR"};
Copy the code

Structural variables can be initialized at the same time they are declared, but the initialized values must be written in the order of the structure members, and the expression used for initialization must be constant, with the remaining members starting with 0.

The value of a structure member is an lvalue, so it can be assigned to the left of the equals sign, or the member ++ (which points to the next member or increases in value);

An lvalue can be used as an rvalue. Part12 is a structural variable! Part2 =part1; Assign the member contents of 1 to 2;

Operations other than assignment cannot be used, especially == or! =;

Type definition:

typedef int B;
B flag;
Copy the code

B can now declare variables, casts, or other variables along with built-in type names;

On the one hand, it makes the program easier to understand (we can declare variables with similar attributes the same), and on the other hand, it makes it easier to change the data type of those variables.

Declaration of structure type:

The repetitive structure information bulges the program, so we name the types of the structure instead of the structure variables.

struct part{. };/ / the first
struct part part1;
struct part part1={... };struct part{. }part1; part1=part2;typedef struct {. }part;/ / the second
part part1;

Copy the code
void printf(struct part p){
struct part p2=p;// This works
p.name;
p.age};// as a parameter
printf(part1);

struct part builidpart(char name){ // Returns a structure
struct part p;
p.name=name;
return p};

Copy the code

Replace structures with Pointers to structures;

Structure Nested structure

struct name{
char firstname[5];
char middlename[5];
char lastname[5];

};
struct p {
struct name name1;
int i;
}p1;
p1.name1.firstname/ / reference
struct name name2;
p1.name1=name2;
Copy the code

Structure Nested structures have the advantage of making it easier to use the properties of members as a unit.

display(p1.name1);

Array with structure type:

struct part index[100]; There are 100 originals;

Assignment: the index [1]. The number = 9;

index[1].name[0]=’\0′;

Struct array initialization:

const struct part index[] ={// The braces are arrays
{"ab".1}// The braces are for the structure, {"cd".2}};
Copy the code

C library function int isspace(int C) checks if the character passed is whitespace. This function returns a non-zero value (true) if c is a whitespace character, and 0 (false) otherwise.

EOF end of file

The joint

There is only one difference between structural variables and union variables: structural variables are stored in different memory addresses, whereas union variables are stored in the same memory address.

Members overlap with each other, changing one member changes the values previously stored between any other members, so think of unions as places where only one member can be stored.

Initialize the

u{
int i;
floatf; }u={0};/ / I 0;
Copy the code

Joint application

1. Save space

Design a gift structure, the gift has books, cups, clothes, corresponding to the information of different items

Can be in the gift structure:

union{
struct{
char title[title_len+1]}book;
struct{}mug;
struct{}cloth; } the item;Copy the code

When called, c(structure name).item (name of the union member of the structure).book(name of the union member of the structure).title(name of the member of the structure)

2. You can create data structures with different data types

typedef union{
int i;
floatF; }Number; Number number_array[10];
number_array[1].i=1;
number_array[2].f=1.0;
Copy the code

Now arrays can also contain different types of members.

Tag field: Find an easy way to determine the last member of a union changed


#define int_kind 0
#define float_kind 1

typedef {
int kind;

enum{int_kind,float_kind}kind;

union{
inti; }u; }n; n.kind =int_kind;// Add this step when assigning to the union member
n.u.i=1;

if(n.kind==int_kind){  }
else // Do this when judging
Copy the code

The enumeration

Unlike structures and unions, enumeration constants must have names different from other identifiers declared in the closure scope.

Enumeration constants are free to choose different values, and it is legal for different enumeration constants to choose the same value.

1.enum suit {a,b,c,d};

enum suit s1,s2;

2.typedef enum {a,b,c,d}suit;

suit s1,s2;

When no value is specified for an enumeration constant, its value is greater than the value of the previous constant, which defaults to 0.

Enum colors{black, gray,orange=15,red};

0,1,15,16;


When trying to use sizeof determine the number of bytes in the structure, the number is greater than the members together the number of bytes, because of some computer for data items from a certain number of bytes (typically four) multiples, char a, b will with 3 bytes after empty (useless bytes), c standard allows only at the back of the members or member of the last hole, The address of the first member is guaranteed to be the same as the address of the entire structure, so it is illegal to use == to determine whether two structures are equal or not. Even if the corresponding members have the same value, the discarded values in the void may be different, and the initialization of the void (making them all 0) affects the performance of the program of the structure.

Multiple files can share structure types by placing structure declarations in header files and including header files where needed.