White to Daniel [12] road again backstage port management to optimize the management of the switch

Again optimization of back office management port switch Item 12
Item succinctly

1. Why should we pointers

The value of the transfer function, not by calling the function, to modify the argument of the function.

2. The definition of a pointer

What is Pointer

It is essentially a pointer address value:

#include <stdio.h>

int main(void){
    int age;

    //定义了一个指针
    //指针是一个变量
    //这个变量的名称是 p
    //这个指针,可以用来指向一个整数!
    //就是说:p的值是一个整数的地址!!!
    int * p;

    //指针p指向了age
    //p的值,就是变量age的地址
    p = &age;

    scanf("%d", p);

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

Define the pointer

int P;
or:
int
P;
or:
int * P;

c language pointer, 4 bytes.

3. initialized pointers, visit

Initialize the pointer
White to Daniel [12] road again backstage port management to optimize the management of the switch
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. 访问(读、写)指针变量本身的值!!!(和其他普通变量的访问方式相同)
    int *p3;
    p3 = p1; //读指针p1的值, 设置指针p3的值

    printf("p1=%d\n", p1); //不建议使用该方式

    //使用16进制打印,把地址值当成一个无符号数来处理的
    printf("p1=0x%p\n", p1);  
    printf("p1=0x%x\n", p1);  
    printf("p1=0x%X\n", p1);  

    return 0;
}

Binary and hexadecimal 16 (Supplement)
10 decimal, binary, hexadecimal

Decimal:
every one, there are 10 states (0,1,2,3,4,5,6,7,8,9), every 10 into 1

Binary:
The computer recognizes only two 229
each, there are two states (0,1)

Hex:
each one, there are 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 some binary data is converted to hexadecimal

For example:
10 hex: 257
Binary: 100000001
Hex: 0x101
access pointer points to content
White to Daniel [12] road again backstage port management to optimize the management of the switch

#include <stdio.h>

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

    int x;
    x = *p;  //*是一个特殊的运算符,*p表示读取指针p所指向的变量的值
    printf("x=%d\n",  x);

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

    return 0;
}

4. null pointer

1. What is the null pointer?

Null pointer, the pointer value is zero.
* P int;
P = 0;

2. Access the consequences of a null pointer
#include <stdio.h>

int main(void) {
    int  *p;
    p = 0;  //p就是一个空指针!

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

    //访问空指针指向的值,将导致程序崩溃!!!
    printf("%d\n", *p); //读取 地址为0的int类型变量

    system("pause");
    printf("程序结束\n");
    return 0;
}

3. Use of empty pointer
1) pointer is initialized to null pointer
example: int P = 0;
recommended for such use:
int
P = NULL;
purpose is to prevent illegal data access.

2) when the pointer no longer in use, may be set to a null pointer
int * my_girl = & xiao_long_lv;
my_girl = NULL;

1) indicates that the pointer points to no specific
int * P = NULL;
! IF (P) {
......
}

The pointer structure

#include <stdio.h>

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

int main(void) {
    struct friend f1 = {
        "小龙女", "女", 18
    };

    //定义了一个指针变量p, 
    //这个my_girl可以指向一个struct friend类型的变量
    struct friend *my_girl;

    my_girl = &f1;

    //直接通过结构体变量来访问该结构体内部的成员
    printf("%s, %s, %d\n", f1.name, f1.sex, f1.age);

    //通过指针p来访问结构体内部的成员
    //方式1, 很少使用该方式
    printf("%s, %s, %d\n", (*my_girl).name, (*my_girl).sex, (*my_girl).age);

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

    return 0;
}

6. character arithmetic

Increment pointer arithmetic

#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]);

    //先使用数组的方式来访问
    for (int i=0; i<len ; i++) {
        printf("第%d个学员的年龄是:%d\n", i+1, ages[i]);
    }

    //使用指针来访问
    //int *p = ages;  //指针p指向ages[0]
    int i = 0;
    for (int *p = ages; p < ages+len ; p++, i++) {
        printf("第%d个学员的年龄是:%d\n", i+1, *p); 
    }

    return 0;
}

Pointer decrement

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

/**
 * 让用户输入一个字符串,然后反向输出(不能改变原来的字符串!)
 *  "12345"  逆转成   "54321"
 */

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

    printf("请输入一个字符串: ");
    gets(line);

    len = strlen(line);
    //方法1 (改变了字符串本身)
    /*
    for (int i=0; i<len/2; i++) {
        tmp = line[i];
        line[i] = line[len-1-i];
        line[len-1-i] = tmp;
    }
    printf("逆转以后:%s\n", line);
    */

    //方法2:不改变字符串
    /*
    for (int i=len-1; i>=0; i--) {
        printf("%c", line[i]);
    }
    */

    //用指针来重写方法2
    char *p1 = line;
    char *p2 = p1 + len -1;
    for (char *p=p2; p>=p1; p--) {  //p--,就相当于:  p=p-1
        printf("%c", *p);
    }

    return 0;
}

Subtraction between pointers and integer

Subtraction between the pointer and the pointer

7. null pointer

Good coding practice: Using a null pointer

8. The pointer to the array

Project requirements

Port management pointer to further optimize the code more concise.

Project realization

Project practice

1. To achieve the reversal of a string containing 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字符,一般都是小于等于127的。
            *p2-- = *p1++;
        } else {
            *(p2-1) = *p1++;
            *p2 = *p1++;
            p2 -= 2;
        }
    }

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

Guess you like

Origin blog.51cto.com/14632565/2462472