[novice road to Daniel 12] port management of switch background management is optimized again

Posted by PHPGuru_2700 on Fri, 27 Dec 2019 23:03:00 +0100

Project 12 re optimization of port management of switch background management
Project presentation

1. Why to use pointer

The function's value is passed, and the function's arguments cannot be modified by calling the function.

2. Pointer definition

What is the pointer

The pointer is essentially an address value:

#include <stdio.h>

int main(void){
    int age;

    //A pointer is defined
    //Pointer is a variable
    //The name of this variable is p
    //This pointer can be used to point to an integer!
    //That is to say: the value of p is an integer address!!!
    int * p;

    //Pointer p points to age
    //The value of p is the address of the variable age
    p = &age;

    scanf("%d", p);

    printf("age=%d\n", age);
    return 0;
}

Definition of pointer

int p;
Or:
int p;
Or:
int * p;

Pointer in c language, occupying 4 bytes.

3. Initialization and access of pointer

Initialization of pointer

demo:

#include <stdio.h>

int main(void) {
    int mygirl = 18;
    int *p1 = &mygirl;

    int *p2 = p1;
    return 0;
}

Pointer access

Access pointer
demo

#include <stdio.h>

int main(void) {
    int mygirl = 18;
    int *p1 = &mygirl;
    int *p2 = p1;

    //1. Access (read, write) the value of pointer variable itself!!! (same as other common variables)
    int *p3;
    p3 = p1; //Read the value of pointer p1 and set the value of pointer p3

    printf("p1=%d\n", p1); //This method is not recommended

    //Using hexadecimal printing, the address value is treated as an unsigned number
    printf("p1=0x%p\n", p1);  
    printf("p1=0x%x\n", p1);  
    printf("p1=0x%X\n", p1);  

    return 0;
}

Binary and hexadecimal (supplementary)
10 base, 2 base, 16 base

10 binary system:
Each bit has 10 states (0,1,2,3,4,5,6,7,8,9), and each bit enters 1 in 10

2 binary system:
Computer only recognizes binary
Each bit has two states (0,1)

16 binary system:
Each bit has 16 states (0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f)
For the convenience of description, we often convert some binary data to hexadecimal representation

For example:
Decimal: 257
Binary: 10000001
Hex: 0x101
What the access pointer points to

#include <stdio.h>

int main(void) {
    int my_girl = 18;
    int *p = &my_girl;

    int x;
    x = *p;  //*Is a special operator, * P represents the value of the variable pointed to by the read pointer p
    printf("x=%d\n",  x);

    printf("*p = %d\n", *p);
    my_girl++;
    printf("*p = %d\n", *p);

    return 0;
}

4. empty pointer

1. What is a null pointer?

A null pointer is a pointer with a value of 0.
int *p;
p = 0;

2. Consequences of accessing null pointer
#include <stdio.h>

int main(void) {
    int  *p;
    p = 0;  //p is a null pointer!

    printf("%p\n", p);

    //Accessing the value pointed to by the null pointer will cause the program to crash!!!
    printf("%d\n", *p); //Read int type variable with address 0

    system("pause");
    printf("Program end\n");
    return 0;
}

3. Use of null pointer
1) Pointer initialized to null pointer
For example: int p = 0;
It is recommended to use:
int p = NULL;
The goal is to avoid accessing illegal data.

2) When the pointer is no longer in use, it can be set to a null pointer
int *my_girl = &xiao_long_lv;
my_girl = NULL;

1) Indicates that this pointer has not been specifically pointed to
int *p = NULL;
if (!p) {
......
}

5. Pointer to structure

#include <stdio.h>

struct friend {
    char name[32];
    char sex[3];
    int age; 
};

int main(void) {
    struct friend f1 = {
        "Little dragon maiden", "female", 18
    };

    //A pointer variable p is defined, 
    //This my girl can point to a variable of type struct friend
    struct friend *my_girl;

    my_girl = &f1;

    //Access the members inside the structure directly through the structure variable
    printf("%s, %s, %d\n", f1.name, f1.sex, f1.age);

    //Access members inside the structure through pointer p
    //Mode 1, rarely used
    printf("%s, %s, %d\n", (*my_girl).name, (*my_girl).sex, (*my_girl).age);

    //Mode 2
    printf("%s, %s, %d\n", my_girl->name, my_girl->sex, my_girl->age);

    return 0;
}

6. Arithmetic operation of characters

Auto increment of pointer

#include <stdio.h>

int main(void) {
    int ages[] = {20,15,16,14,23,28,30,38, 35, 32, 26};
    int len = sizeof(ages) / sizeof(ages[0]);

    //First use array to access
    for (int i=0; i<len ; i++) {
        printf("The first%d The age of the students is:%d\n", i+1, ages[i]);
    }

    //Using pointers to access
    //int *p = ages; / / pointer p points to ages[0]
    int i = 0;
    for (int *p = ages; p < ages+len ; p++, i++) {
        printf("The first%d The age of the students is:%d\n", i+1, *p); 
    }

    return 0;
}

Self subtraction of pointer

#include <stdio.h>
#include <string.h>

/**
 * Let the user input a string and output it in reverse (the original string cannot be changed! )
 *  "12345"  Reverse to "54321"
 */

int main(void) {
    char line[128];
    int len;
    char tmp;

    printf("Please enter a string: ");
    gets(line);

    len = strlen(line);
    //Method 1 (changes the string itself)
    /*
    for (int i=0; i<len/2; i++) {
        tmp = line[i];
        line[i] = line[len-1-i];
        line[len-1-i] = tmp;
    }
    printf("After reversal:% s\n", line);
    */

    //Method 2: do not change the string
    /*
    for (int i=len-1; i>=0; i--) {
        printf("%c", line[i]);
    }
    */

    //Rewrite method 2 with pointer
    char *p1 = line;
    char *p2 = p1 + len -1;
    for (char *p=p2; p>=p1; p--) {  //p -- is equivalent to: p=p-1
        printf("%c", *p);
    }

    return 0;
}

Addition and subtraction between pointer and integer

Addition and subtraction between pointers

7. empty pointer

Good coding habit: use null pointer

8. Pointer to array

Project requirements

Use pointers to further optimize port management and make the code more concise.

Project realization

Project practice

1. Implement string reversal with Chinese characters

void reverse(unsigned char *s) {
    int len = strlen(s);
    unsigned char tmp[len+1];

    unsigned char *p1 = s; 
    unsigned char *p2 = tmp + len;

    *p2-- = 0;
    while (*p1) {
        if (*p1 < 0xA0) { //ASCII characters, generally less than or equal to 127.
            *p2-- = *p1++;
        } else {
            *(p2-1) = *p1++;
            *p2 = *p1++;
            p2 -= 2;
        }
    }

    for (int i=0; i<len; i++) {
        s[i] = tmp[i];
    }
}

Topics: C++ ascii less C