Learn more about pointers

1. We know that the pointer is a variable, which stores the address of a variable,
The address can uniquely identify a space
 2. Pointer size is fixed, 32 bits: 4 / 64 bits: 8 bytes
 3. Pointer has type, type determines pointer + - integer step length, dereference permission
 4. Pointer has operation

Character pointer

//As the name implies, a character pointer points to a character type.
int main()
{
	char ch = 'w';
	char* pc = &ch;
	printf("%c\n",*pc);//The output is' w '
	return 0;
}

//You can see another
int main()
{
	char* pstr = "hello world";//But notice here, is the string put into the inside of the big pointer?
	//Actually, it's not. The first address of the string is put into pstr. It's not unreasonable to put the string into pstr
	//Note that the string is a string constant stored in the memory constant area,
	printf("%s\n",*pstr);
	return 0;//The output here is helloworld ""
}

Pointer array

Note: pointer array is an array for storing pointers, its essence is still an array;

int *arr1[10];//[] prior to arr1 indicates that it is an array, with the number of data types int stored inside*
char* arr2[4];//First level character pointer array
char** arr3[4];//Second level character pointer array

Array pointer

Note: array pointer is a pointer to array, its essence is still pointer;

int *p1[10];//Pointer array
int (*p2)[10];//Array pointer

//Because the priority of [] is higher than *, p2 and * are now combined, indicating that p2 is a pointer, and then points to an array with storage shape and size of 10,

When it comes to arrays, let's talk about it & the difference between array names and array names
It can be seen that although the value of array name is the same as that of & array name, its meaning is different;
In fact; & arr means the address of the array, not the address of the first element of the array; array address + 1, skip the whole array size, array is the address of the first element + 1, skip one element size;
Learn about array pointers and pointer arrays. Take a look at the code:

int arr[5];//Shaping array
int *parr1[10];//Pointer array, storing pointer of int *;
int (*parr2)[10];//A numeric pointer to an integer array of size 10
int (*parr3[10])[5];//Pointer array, which stores an integer array with a pointer size of 5, and the pointer array itself has a size of 10;

Array parameter and pointer parameter

1-D and 2-D array parameters:

//One dimensional array:
void test(int arr[])//ok
{}
void test(int arr[10])//ok
{}
void test(int *arr)//ok
{}
void test2(int *arr[20])//ok
{}
void test2(int **arr)//ok
{}

//Two-dimensional array
void test(int arr[3][5])//ok
{}
void test(int arr[][])//ok? Obviously not ok
{}
void test(int arr[][5])//ok
{}
//Summary: the design of two-dimensional array parameters and function parameters can only omit the first [] number.
//Because for a two-dimensional array, you don't know how many rows there are, but you have to know how many elements there are in one row.
//This is convenient for calculation.
void test(int *arr)//ok
{}
void test(int* arr[5])//ok
{}
void test(int(*arr)[5])//ok
{}
void test(int **arr)//ok
{}

First and second level pointer parameter transfer

//Primary pointer
void print(int *p, int sz)
{
	int i = 0;
	for(i=0; i<sz; i++)
	{
		printf("%d\n", *(p+i));
	}
}
//Secondary pointer - Advanced
void test(int** ptr)
{
	printf("num = %d\n", **ptr);
}

Note: when a function passes a parameter, the actual parameter - 'parameter is a copy;
When a function is called, the compiler opens up space for parameters and copies only one copy of the arguments to the parameters.
When passing the first level pointer, it is only effective to operate on the memory variable pointed by the pointer;
When passing the second level pointer, it is only effective to change the pointer's direction;

Function pointer


It can be seen from the picture that these two addresses are the addresses of the test function, but how to save the address of the function requires a function pointer:

void test()
{
	printf("hello\n");
}

void(*pfun1)();//Function pointer; indicates that pfun1 is a pointer to a parameterless. Function without return value

//This is also a function pointer
void (*signal(int , void(*)(int)))(int);
//To simplify it
typedef void(*pfun_t)(int);
pfun_t signal(int,pfun_t);

Function pointer array

Array is a combination of elements of the same type, pointer array storage is a pointer of the same type, then function pointer array storage is a function pointer of the same function type;

int (*parr1[10])()
The combination of parr1 and [] indicates that it is an array,
The content of array is int(*) ()

Callback function

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

Author: no.body
Link: https://www.zhihu.com/question/19801131/answer/27459821
Source: Zhihu

#include <stdio.h>
//The user of qosrt function has to implement a comparison function
int int_cmp(const void * p1, const void * p2)//Callback function
{
return (*( int *)p1 > *(int *) p2);
}
int main()//Main function
{
int arr[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
int i = 0;
qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof (int), int_cmp);//Intermediate function
for (i = 0; i< sizeof(arr) / sizeof(arr[0]); i++)
{
printf( "%d ", arr[i]);
}
printf("\n");
return 0;
}
45 original articles published, 15 praised, 9363 visited
Private letter follow

Posted by linkin on Sat, 14 Mar 2020 21:24:32 -0700