in front , We know some basic information about pointers :

         A pointer is a variable . What we say verbally refers to pointer variables , It is used to store addresses ;

         The size of the pointer is fixed , stay 32 On the bit platform 4 Bytes , stay 64 Yes 8 Bytes ;

         Pointer has data type , The data type of the pointer determines where the pointer is +- Step size when integer ;

This article mainly discusses some advanced uses of pointers .

one , Character pointer

         In pointer type , One type of pointer is called character pointer (char*);

Let's look at these two situations :

char ch = 'w';    

char *pc = &ch;

*pc = 'w';

printf("%c", *pc);//w

This situation is printed directly w;

char* pstr = "hello world.";// Here is to put a string into pstr Is it in the pointer variable ?

* pstr = 'w';   

printf("%s\n", pstr); 

What does this look like ?

         Run up and you'll find , The program crashed . that is because , above "hello world." Is a read-only data area stored in memory
of , in other words , This string constant cannot be modified . usually , For constants , add const Keyword to protect the constant so that it cannot be modified .

To impress , Let's take a look at such a problem :
#include <stdio.h> // Judge the result of the code int main() { char str1[] = "hello bit.";
char str2[] = "hello bit."; char *str3 = "hello bit."; char *str4 = "hello
bit."; if(str1 ==str2) printf("str1 and str2 are same\n"); else printf("str1
and str2 are not same\n"); if(str3 ==str4) printf("str3 and str4 are same\n");
else printf("str3 and str4 are not same\n"); return 0; }

         here str3 and str4 Refers to the same constant string .C/C++ The constant string is stored in a separate memory area . When several pointers point to the same string , They actually point to the same block of memory . But use the same constant string to initialize
Different memory blocks will be created when different arrays are . therefore str1 and str2 Different ,str3 and str4 identical .

When defining pointer variables , There is often a misunderstanding :

int* pa, pb;

         The original idea was to define a pointer pa, Pointer pb, But this definition is wrong . only pa It's a pointer , and pb Just ordinary variables .

Look at the following code :

typedef int* pint;

#define PINT int*

...

PINT pa, pb;

pint pc, pd;

         In this case ,pa,pc,pd Pointer variable , and pb Is a normal variable .define Preprocessing is just a simple replacement , There is no other way ; and typedef A keyword is a new name for a type . such ,pc,pd All become pointer types .

two , Pointer array

         What is a pointer array ? The array that holds the pointer is the pointer array . for instance , In an integer array , Stored are some integers , In the character array , Stored characters , Analog pointer array , What is stored inside is the pointer .

  Define a character pointer array :
char* arr[] = { "abcdef", "qwer", "zhangsan" }; int i = 0; int sz =
sizeof(arr) / sizeof(arr[0]); for (i = 0; i < sz; i++) { printf("%s\n",
arr[i]); }

  give the result as follows : 

                 

three , Array pointer

         Array pointers are pointers , It represents a pointer to an array . We define int(*p)[10], It should be noted that ,[] Priority over *, So we need to add (). This definition :p and * combination , Indicates that this is a pointer , Then point to the size 10 An array of integers , in other words , This pointer points to a size of 10 individual int Array of types

  It should be noted that , The first address of the array points to it , Is not the address of the first element of the array . Let's look at such a set of codes :

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

The result is :

         We found ,  Array name and & The array name and the printed address are the same . But add them 1 after ,arr Yes, skip 4 Size of bytes , and &arr Yes, I skipped 4*10 Size of bytes . We found , actually &arr and arr, Although the value is the same , But the meaning is different .  actually &arr
Represents the address of the array , Not the address of the first element of the array . Address of array +1, Skip the size of the entire array , therefore &arr+1 be relative to &arr The difference is 40.

Array pointers are generally used to transfer parameters of two-dimensional arrays . To make a difference , We use one-dimensional arrays and two-dimensional arrays for comparison .

int arr[10] = {1,2,3,4,5,6,7,8,9,0};    

int (*p)[10] = &arr;// Put array arr The address of is assigned to the array pointer variable p, But there's no need to bother , I wouldn't write that , This is just a comparison

printf("%d", **p);//*p Indicates the address of the array , stay * I got the contents in a minute 1;

//printf("%d", *((*p + 0) + 0));//(*)

printf("%d", *(*p + 1));//2

         Define a size of 10 Array of , Defining an array pointer , Point to size 10 Array of , then &arr operation , Get the address of this array . It should be noted that :arr Is the address of the first element of the array
,  and p Is a pointer , Why not directly int(*p)[10] =
arr And ? there p Is the address to the array ,arr Is the address of the first element of the array , Is not the address of the array , The address of the array is &arr. Look at the two-dimensional situation :

int arr[3][5] = { {1,2,3,4,5}, {6,7,8,9,0}, {11,12,13,14,15} };

int(*p)[5] = arr;
print(arr, 3, 5);
printf("\n");

for (int i = 0; i < 3; ++i)
{
    for (int j = 0; j < 5; ++j)
    {
        printf("%d ", *(*(p + i) +
j));//p+i Represents the second i Address of the line , stay *, He took out this line , stay +j, That's it j Address of the first element , stay *, Took out this element
    }
    printf("\n");

}

         here arr be missing &, But it's the same ,arr Is the address of the first element of the array ,
The address of the first element of the two-dimensional array is the first row , Is also an array , that , There's no need to add it &, Otherwise, an error will be reported . In essence , If we compare the above one-dimensional array to a two-dimensional array , Then its printing is (*).

four , Function pointer

         A function pointer is a pointer to a function . definition Void
(*pfun1)(); express pfun1 Is a pointer , Pointer to a function , This function has no arguments , The return value is void. Let's look at the following code :

#include void test()

{

        printf("hehe\n");

}

int main()

{

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

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

        return 0;

}

  Two addresses are output , These two addresses are test Address of the function . In essence , this & It doesn't matter whether you add it or not .

         The results are 5, and , The compiler has no errors or warnings , That means , They are equivalent . In essence ,
For function names , ahead & and * Will be ignored , Front plus or not & or * It makes no difference . Let's look at this code next :

(*( void (*)())0)();// come from <<C Language traps and defects >>

          It means : hold 0 Cast type to void (*)() Function pointer of type , Call after dereference 0 This parameter at the address is parameterless , Return type is void Function of .

five , Function pointer array

         Store the address of the function in an array . Then this array is called function pointer array . We define int(*p[10])() Such a function pointer array , It means :[] and p combination , explain p Is an array , Added one *, Indicates that it is an array of pointers , Adding one (), This is a function , The arguments to the function are parameterless , meanwhile ,int Indicates that its return value is int type .

         A function pointer array is used as a transfer table . such as , Let's make a calculator with two operands , The code is as follows ,switch Can part of the code be simplified ? Yes , You can do this with a function pointer array .
#include <stdio.h> int add(int a, int b) { return a + b; } int sub(int a, int
b) { return a - b; } int mul(int a, int b) { return a*b; } int div(int a, int
b) { return a / b; } int main() { int x, y; int input = 1; int ret = 0; do {
printf( "*************************\n" ); printf( " 1:add 2:sub \n" ); printf( "
3:mul 4:div \n" ); printf( "*************************\n" ); printf( " Please select :" );
scanf( "%d", &input); switch (input) { case 1: printf( " Input operand :" ); scanf( "%d
%d", &x, &y); ret = add(x, y); printf( "ret = %d\n", ret); break; case 2:
printf( " Input operand :" ); scanf( "%d %d", &x, &y); ret = sub(x, y); printf( "ret =
%d\n", ret); break; case 3: printf( " Input operand :" ); scanf( "%d %d", &x, &y); ret =
mul(x, y); printf( "ret = %d\n", ret); break; case 4: printf( " Input operand :" );
scanf( "%d %d", &x, &y); ret = div(x, y); printf( "ret = %d\n", ret); break;
case 0: printf(" Exit program \n"); break; default: printf( " Selection error \n" ); break; } } while
(input); return 0; } // Function pointer array implementation #include <stdio.h> int add(int a, int b) {
return a + b; } int sub(int a, int b) { return a - b; } int mul(int a, int b) {
return a*b; } int div(int a, int b) { return a / b; } int main() { int x, y;
int input = 1; int ret = 0; int(*p[5])(int x, int y) = { 0, add, sub, mul, div
}; // Transfer table while (input) { printf( "*************************\n" ); printf( "
1:add 2:sub \n" ); printf( " 3:mul 4:div \n" ); printf(
"*************************\n" ); printf( " Please select :" ); scanf( "%d", &input); if
((input <= 4 && input >= 1)) { printf( " Input operand :" ); scanf( "%d %d", &x, &y); ret
= (*p[input])(x, y); } else printf( " Incorrect input \n" ); printf( "ret = %d\n", ret); }
return 0; }
six , Pointer to array of function pointers ( Simple understanding )

         The pointer to the array of function pointers is a pointer , Pointer to an array , Array elements are function pointers . It is defined as follows :

void test(const char* str)

{

        printf("%s\n", str);

}

int main()

{

        // Function pointer pfun

        void (*pfun)(const char*) = test;

        // Array of function pointers pfunArr

        void (*pfunArr[5])(const char* str);

        pfunArr[0] = test;

        // Pointer array to function pfunArr Pointer to

        ppfunArr void (*(*ppfunArr)[10])(const char*) = &pfunArr;

        return 0;

}

Technology