1. Introduction

Pointers are introduced as a key point in many books. As the soul of C language, Pointers are ubiquitous in projects.

For example, when a pointer is used as a function parameter, it can modify the data in the source address indirectly, which is equivalent to the problem that the function return can only return one value at a time.

Pointer is the most intuitive to use in embedded and single chip microcomputer. It can directly access register address and configure register through pointer. The CPU and peripheral hardware of a computer depend on address operation.

2. How to define pointer variables?

#include <stdio.h>
#include <string.h>
int main(a)
{
    char *p1;  // Define a pointer variable of type char
    int *p2;   // Define a pointer variable of type int
    // a pointer variable of type int holds address data.
    
    int data=123; // Define a variable of type int
    // A variable of type int: stores integer data.
    return 0;
}
Copy the code

3. How to use Pointers?

#include <stdio.h>
#include <string.h>
int main(a)
{
    // &data fetches the address a&b bit with a&&b and
    // int *p defines a pointer of type int
    // *p; // *p
    // a times b
    
    int *p; // The pointer variable itself holds the address
    int data=123; 
    //p=data; // Error: Data itself is only a variable identifier, not an address
    p=&data;  // Assign the address of data to the pointer p
    
    printf("*p=%d\n",*p); // retrieve the pointer p to the data in space
    *p=Awesome!; // Assign to the space to which the pointer points
    printf("data=%d\n",data);
    return 0;
}
Copy the code

4. What is a wild pointer?

A wild pointer is an invalid pointer that has no valid address space of its own.

Common error with wild Pointers: segment errors.

The following code causes a segment error:

#include <stdio.h>
#include <string.h>
int main(a)
{
    int *p;// The pointer can only hold addresses
    *p=123;
    printf("*p=%d\n",*p);
    return 0;
}
Copy the code

5. How much space does the pointer itself take up?

All pointer types take up four bytes. It has nothing to do with type.

#include <stdio.h>
#include <string.h>
int main(a)
{
    int *int_p;// The pointer can only hold addresses
    int int_a;
    char *char_p;// The pointer can only hold addresses
    char char_a;
    float *float_p;// The pointer can only hold addresses
    float float_a;
    double *double_p;// The pointer can only hold addresses
    double double_a;
    printf("int_a=%d\n".sizeof(int_a));/ / 4
    printf("int_p=%d\n".sizeof(int_p));/ / 4
    
    printf("char_a=%d\n".sizeof(char_a));
    printf("char_p=%d\n".sizeof(char_p));
    
    printf("float_a=%d\n".sizeof(float_a));
    printf("float_p=%d\n".sizeof(float_p));
    
    printf("double_a=%d\n".sizeof(double_a));
    printf("double_p=%d\n".sizeof(double_p));
    return 0;
}
Copy the code

6. Pointers access array members

6.1 What are the ways to access members of a one-dimensional array?

#include <stdio.h>
#include <string.h>
int main(a)
{
    int a[]={1.2.3.4.5.6.7.8.9.0};
    int *p;
    p=a;
    printf("Subscript 4 value :%d\n",a[4]); / / 5
    printf("Subscript 4 value :%d\n",*(a+4)); / / 5
    printf("Subscript 4 value :%d\n",*(p+4)); / / 5
    return 0;
}
Copy the code

6.2 The common operation mode for Pointers to access one-dimensional data is string

#include <stdio.h>
#include <string.h>
int main(a)
{
    char str[]="1234567890";
    char *p=str;
    int len=0;
    printf("p=%s,str=%s\n",p,str);
    // Calculates the length of the string
    while(*p++! ='\ 0')
    {
        len++;
    }
    printf("len=%d\n",len);
    return 0;
}
Copy the code

6.3 Pointer Increment and Decrement? The ++ and – operators

#include <stdio.h>
#include <string.h>
int main(a)
{
    int a[]={1.2.3.4.5.6.7.8.9.0};
    int *p=a;
    printf("%d\n",*p);   / / 1
    printf("%d\n",*p++); / / 1
    printf("%d\n",*p);   / / 2
    printf("%d\n",*(p+3));/ / 5
    printf("%d\n",*p);/ / 2
    printf("%d\n",*(++p));/ / 3
    printf("%d\n",*p);/ / 3
    printf("%d\n",*p--);/ / 3
    printf("%d\n",*p);/ / 2
    return 0;
}
Copy the code

6.4 How many offset bytes are the pointer autoincrement and autodecrement?

Is related to the type of the pointer itself.

#include <stdio.h>
#include <string.h>
int main(a)
{
    / * int a [] =,2,3,4,5,6,7,8,9,0 {1}; int *p=a; Printf (" offset address :p=%#x\n",p); *(p++); printf("%d\n",*p); //2 printf(" offset address :p=%#x\n",p); * /
    char a[]={1.2.3.4.5.6.7.8.9.0};
    char *p=a;
    printf("Address before offset :p=%#x\n",p);
    *(p++);
    printf("%d\n",*p);   / / 2
    printf(Offset address :p=%#x\n,p);
    return 0;
}
Copy the code

6.5 Can arrays themselves be used as Pointers?

Can not be

Pointer features: self – increasing, self – decreasing, can change the point.

#include <stdio.h>
#include <string.h>
int main(a)
{
    int a[]={1.2.3.4.5}; //*(a+3)
    int b[]={11.12.13.14.15};
    // The following three lines of code are incorrect
    a--;
    a++;
    a=b;
    return 0;
}
Copy the code

6.6 Can Pointers be Used as Arrays?

can

Array features: Array members can be accessed by subscript.

#include <stdio.h>
#include <string.h>
int main(a)
{
    int a[]={1.2.3.4.5};
    int *p=a;
    printf("%d\n",a[0]);
    printf("%d\n",p[0]); //*p
    return 0;
}
Copy the code

Array Pointers

Array Pointers can point to a one-dimensional array. Row Pointers are also called one-dimensional array Pointers.

Definition syntax :char (*p)[5]; This pointer points to the address of a one-dimensional array in a two-dimensional array, whose members are five bytes.

Char a[10][5];

#include <stdio.h>
#include <string.h>
int main(a)
{
    int (*p)[5]; // Define a one-dimensional array pointer -- row pointer
    int a[2] [5]=
    {
        {1.2.3.4.5},
        {6.7.8.9.10}}; p=a;// give the address a to the pointer p
    printf("%d\n",p[0] [2]);
    
    p++; // Add the size of a one-dimensional array
    printf("%d\n",p[0] [2]);
    return 0;
}
Copy the code

Pointer arrays

Pointer array: Indicates that the array members can hold Pointers.

Pointer array definition syntax: int *p[5]; Defines an array of ints that can hold up to five pointer variables (addresses) of int type.

#include <stdio.h>
#include <string.h>
int main(a)
{
    int a,b,c;
    int *p[3]; // Define three pointer variables of type int at once
    // Store the address
    p[0]=&a;
    p[1]=&b;
    p[2]=&c;
    // Assign to space
    *p[0] =123;
    *p[1] =456;
    *p[2] =789;
    // Retrieve data
    printf("%d\n",*p[0]);
    printf("%d\n",*p[1]);
    printf("%d\n",*p[2]);
    return 0;
}
​
#include <stdio.h>
#include <string.h>
int main(a)
{
    char a[][100] = {"Xiao Ming"."White"."Xiao li"};
    char *p[]={"Xiao Ming"."White"."Xiao li"};
    
    printf("%s\n",a[0]);
    printf("%s\n",p[0]);
    return 0;
}
Copy the code

9. Common initialization value for pointer types: NULL

#include <stdio.h>
#include <string.h>
int main(a)
{
    char *p=NULL; // (void*)0 == NULL
    printf("%#x\n",p);
    if(p! =NULL)
    {
        // Check whether the pointer can be used
    }
    return 0;
}
Copy the code