[C language] super detailed explanation ☀️ What kind of needle is the pointer? (solve the pointer problem at one time) (advanced level of pointer)

Keywords: C C++ Programming Programmer

What is the needle in front? This article has written the basic knowledge of pointers, and then through this article to have a deeper understanding of pointers

catalogue

  1, Character pointer

  2, Pointer array

3, Array pointer

Array name and & array name

Use of array pointers

4, Function pointer

Function address

Function pointer creation

Use of function pointers

5, Function pointer array

6, Pointer to array of function pointers

  1, Character pointer

  The creation of character pointer is also very simple. char * type can access up to one byte of space at a time

See the following code:

#include <stdio.h>
int main()
{
	char s = 'm';
	char* ps = &s;
	printf("%c\n", *ps);
	*ps = 'w';
	printf("%c\n", *ps);
	return 0;
}

This is the pointer creation and access operation. The operation results are as follows:

  The main thing about character pointers is the following

int main()
{
	char* p = "abcdef";
}

  Is the string "abcdef" saved in the pointer p?

abcdef has a total size of 7 bytes, while char * has only 4 bytes on the 32-bit platform. It can't be stored at all, so it doesn't directly store the whole string in the pointer p

Note that the first character address of this string is given to p

 

This writing method is not too rigorous. Under some compilers, errors will be reported. Please modify it again

int main()
{
	const char* p = "abcdef";
}

  2, Pointer array

  As the name suggests, a pointer array is an array of pointers

Look at the array in the following code

int* arr1[6];     //Array of integer pointers
char* arr2[8];    //Array of first level character pointers
char** arr3[7];   //Array of secondary character pointers

Next, create an integer pointer array

#include <stdio.h>
int main()
{
	int a = 0;
	int b = 1;
	int c = 2;
	int* arr[3] = { &a,&b,&c };
	int i = 0;
	for (i = 0; i < 3; i++)
	{
		printf("%d ", *arr[i]);
	}
}

Here, an integer pointer array is created and initialized. The addresses of a, B and C are stored in it, and then a, B and C are printed by dereference

Operation results:

  As shown below:

 

  Each element in this array is an integer pointer

Deepen your understanding through the following code

#include <stdio.h>
int main()
{
	int arr1[] = { 1,2,3 };
	int arr2[] = { 4,5,6 };
	int arr3[] = { 7,8,9 };
	int* arr[] = { arr1,arr2,arr3 };
	int i = 0;
	for (i = 0; i < 3; i++)
	{
		int j = 0;
		for (j = 0; j < 3; j++)
		{
			printf("%d ", arr[i][j]);
		}
	}
	return 0;
}

Create a pointer array through this code, access it, and print

3, Array pointer

Array pointer is a pointer to the array, which stores the address of the array

The form of array pointer is as follows:

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int(*p)[10] = &arr;
}

p here is first combined with * to show that it is a pointer. What is it pointing to? It points to an array with 10 elements, and the type of each element is int

Array name and & array name

The array name is the address of the first element of the array, and the & array name is the address of the whole array

See the following code:

#include <stdio.h>
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	printf("%p\n", arr);
	printf("%p\n", &arr[0]);
	printf("%p\n", &arr);
	return 0;
}

Operation results:

Through this running result, you will find that the three printed values are the same

  Are array names and & array names really the same? Let's take another look through the following code:

#include <stdio.h>
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	printf("%p\n", arr);
	printf("%p\n", arr + 1);
	printf("%p\n", &arr);
	printf("%p\n", &arr + 1);
	return 0;
}

Operation results:

  You can see that the printed results are different after giving & arr and arr+1 respectively. arr+1 skips one integer and four bytes, while & arr+1 directly skips the whole array

Use of array pointers

The use of array pointers is generally used for two-dimensional arrays

The following code: access the two-dimensional array through the array pointer

#include <stdio.h>
print(int (*p)[5], int x, int y)
{
	int i = 0;
	for (i = 0; i < x; i++)
	{
		int j = 0;
		for (j = 0; j < y; j++)
		{
			printf("%d ", (*(p + i))[j]);
		}
		printf("\n");
	}
	
}
int main()
{
	int arr[3][5] = { {1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7} };;
	//Access the array as a pointer through the following function
	print(arr, 3, 5);
}

Operation results:

4, Function pointer

A function pointer is a pointer to a function -- the address where the function is stored

Function address

  We can first experience it through the following code:

#include <stdio.h>
void test()
{

}
int main()
{
	printf("%p\n", &test);
}

Operation results:

The address of the function is printed here

To know that the array name represents the address of the first element of the array, will the function name also be the address of the function?

  Verify with the following code:

#include <stdio.h>
void test()
{

}
int main()
{
	printf("%p\n", &test);
	printf("%p\n", test);
}

  You can see that the printed results are the same, so you can determine that the function name represents the address of the function

Function name and & function name are the addresses of functions, and there is no difference

Function pointer creation

The following code:

#include <stdio.h>
int Add(int x, int y)
{
	return x + y;
}
int main()
{
	int (*ps)(int, int) = Add;
	return 0;
}

Here * p indicates that ps is a pointer, the following (int,int) indicates that the pointer points to a function, and the previous int indicates that the return value of the function is int

Use of function pointers

We can call the pointed function through the function pointer

The following code:

#include <stdio.h>
int Add(int x, int y)
{
	return x + y;
}
int main()
{
	int (*ps)(int, int) = Add;
	int add = (*ps)(1, 2);
	printf("%d\n", add);
	return 0;
}

Operation results:

  In fact, we put Add in ps. in fact, ps and Add should be the same. In fact, the * used here has no practical effect. We can also use the following writing method

int add = ps(1, 2);

5, Function pointer array

It stores function pointers

The following code:

int add(int x, int y)
{
	return x + y;
}
int sub(int x, int y)
{
	return x - y;
}
int main()
{
	//Function pointer array
	int (*pf[5])(int, int) = { add,sub };
}

In the above code is the function pointer array

pf[5] indicates that it is an array. The rest indicates that each element in it is a function pointer. The function pointed to is two parameters of type int, and the return type bit is int

Function pointer array is mainly used to transfer tables

Before C language implementation of simple calculator This article has written the usage of function pointer array and uses transfer table to realize simple calculator. You are welcome to read it. We won't move that code here

6, Pointer to array of function pointers

This is a pointer. The pointer points to an array. Each element in the array is a function pointer

Because there are very few pointers to the function pointer array, we won't expand the introduction here

The following code example:

    int (*psa)(int, int);    //Function pointer
	int (*ps[5])(int, int);   //Function pointer array
	int (*(*pps)[5])(int, int) = &ps;    //Pointer to array of function pointers

Note: how to judge which type of pointer is mainly depends on the combination and priority of operators

-----------------------------------------------------------------

-----------------Partial completion of C language operators-------------------

About C language, each knowledge point will be introduced in more detail in a separate blog

Welcome to pay attention!!!

Learn and communicate together!!!

Let's finish the programming!!!

--------------It's not easy to organize. Please support the third company------------------

Posted by danwatt on Fri, 24 Sep 2021 16:49:02 -0700