[C Language] Pointer Summary


insert image description here

1. What is a pointer

A pointer is an address, and a pointer in spoken language usually refers to a pointer variable. We can use & (address operator) to take out the memory start address of the variable, and store the address in a variable, which is a pointer variable.

How big is a pointer variable?

  • On a 32-bit machine, the address is a binary sequence composed of 32 0s or 1s, and the address must be stored in 4 bytes, so the size of a pointer variable should be 4 bytes.
  • Then if on a 64-bit machine, if there are 64 address lines, the size of a pointer variable is 8 bytes to store an address.

2. Pointer type

The way the pointer is defined is:**type + ***

The pointer of char* type is used to store the address of char type variable.
The short* type pointer is used to store the address of the short type variable.
The pointer of type int* is used to store the address of variable of type int.

What is the point of the pointer type?

1. The type of pointer determines how far the pointer takes one step forward or backward (distance).

int main()
{
    
    
 int n = 10;
 char *pc = (char*)&n;
 int *pi = &n;
 
 printf("%p\n", &n);
 printf("%p\n", pc);
 printf("%p\n", pc+1);
 printf("%p\n", pi);
 printf("%p\n", pi+1);
 return  0;
}

insert image description here
It can be seen from the running results that the char type pointer plus 1 moves backward by 1 byte, and the int type pointer plus 1 moves backward by 4 bytes.

2. The type of the pointer determines how much authority (how many bytes can be manipulated) when dereferencing the pointer.

#include <stdio.h>
int main()
{
    
    
 int n = 0x11223344;
 char *pc = (char *)&n;
 int *pi = &n;
 *pc = 0;   //观察执行这两句时内存的变化
 *pi = 0;   
 return 0;
}

insert image description here

It can be seen from the result that the pointer dereference of char* can only access one byte, while the dereference of the pointer of int* can access four bytes.

Three, wild pointer

The wild pointer is the position pointed by the pointer is unknown (random, incorrect, not clearly limited)

Reasons for wild pointers:

  1. pointer is not initialized
#include <stdio.h>
int main()
{
    
     
 int *p;//指针未初始化,默认为随机值
 return 0;
}
  1. pointer out of bounds access
#include <stdio.h>
int main()
{
    
    
    int arr[10] = {
    
    0};
    int *p = arr;
    int i = 0;
    for(i=0; i<=11; i++)
   {
    
    
        //当指针指向的范围超出数组arr的范围时,p就是野指针
        *(p++) = i;
   }
    return 0;
}
  • The space pointed to by the pointer is freed

Fourth, the secondary pointer

A pointer variable is also a variable, a variable has an address, and the address of a pointer variable is called a secondary pointer.
insert image description here

  • *pp finds pa by dereferencing the address in pp, and *pp actually accesses p.
  • **pp first finds p through *pp, and then dereferences p: *p, then finds a

Five, the character pointer

In the pointer type, we call the pointer type char* a character pointer

char str1[]="Hellow world";
char str2[]="Hellow world";

const char* str3="Hellow world";
const char* str4="Hellow world";

insert image description here

The general usage of character pointers is:

int main()
{
    
    
 char ch = 'w';
 char *pc = &ch;
 *pc = 'w';
 return 0;
}

There is another way to use:

int main()
{
    
    
 const char* pstr = "hello world.";//这一句代码意思是将首字符的地址放在了patr上,而不是把字符串
 printf("%s\n", pstr);
 return 0;
}

6. Array pointer

definition

  • array of pointers: an array of pointers
  • Array pointer: store the address of the array
int* p1[10];//指针数组
int(*p2)[10];//数组指针

Explanation for int* (p2) [10]
: p is combined with p first , indicating that p is a pointer variable, and then it points to an array of 10 integers. So p is a pointer pointing to an array, called an array pointer.
Note here: The priority of [ ] is higher than that of *, so () must be added to ensure that p is combined with * first.

array name

int arr[10];

The array name usually indicates the address of the first element of the array, with two exceptions:

  1. sizeof(array name)
  2. &Array name
    In both cases, the array name represents the entire array
#include <stdio.h>
int main()
{
    
    
 int arr[10] = {
    
     0 };
 printf("arr = %p\n", arr);
 printf("&arr= %p\n", &arr);
 printf("arr+1 = %p\n", arr+1);
 printf("&arr+1= %p\n", &arr+1);
 return 0;
}

insert image description here
From the program running results, we can see that &arr and arr have the same value, but their meanings are different.
&arr represents the address of the array, not the address of the first element of the array.
The type of &arr is: int(*)[10], which is an array pointer type.
The address of the array + 1 skips the size of the entire array, so &arr+ 1 differs from &arr by 40

Seven, function pointer

#include <stdio.h>
void test()
{
    
    
 printf("Hwllow world");
}
int main()
{
    
    
 printf("%p\n", test);
 printf("%p\n", &test);
 return 0;
}

insert image description here
It can be seen that the function name also represents the address of the function, which is the same as the & function name, so how should the address of the function be stored in the pointer?

insert image description here

Callback

A callback function is a function that is called through a function pointer. If you pass a function pointer (address) as a parameter to another function, when this pointer is used to call the function it points to, we say this is a callback function. The callback function is not directly called by the implementer of the function, but is called by another party when a specific event or condition occurs, and is used to respond to the event or condition.

Guess you like

Origin blog.csdn.net/zcxyywd/article/details/131604208