[C language] advanced pointer (1)

 In the previous articles, we have finished learning the basic content of pointers. In this issue, we will start to learn the advanced part of pointers.

Pointer primary article entry:  Pointer primary

Table of contents

Overview of key knowledge

Early review

character pointer

array of pointers

array pointer

Definition of array pointer

& array name VS array name

Use of array pointers

Array parameters and pointer parameters

One-dimensional array parameter passing

Two-dimensional array parameter passing

First-level pointer parameter passing

Secondary pointer parameter passing


Overview of key knowledge

Full highlights from the Advanced Pointers section:

  • 1. Character pointer
  • 2. Array pointer
  • 3. Array of pointers
  • 4. Array parameter passing and pointer parameter passing
  • 5. Function pointers
  • 6. Array of function pointers
  • 7. Pointer to array of function pointers
  • 8. Callback function
  • 9. Pointers and Arrays Interview Questions

We will probably have 3~4 articles to complete these contents, and welcome your likes and collections.

Early review

The origin of the pointer:

In the computer memory, memory units are divided one by one, and each memory unit has an independent number. These numbers are also called addresses, and addresses are called pointers in C language. The pointer (address) needs to be stored in a variable, and this variable is called a pointer variable.

 We have already touched the "Pointer" chapter in the primary stage, and we know the concept of pointers:

1. A pointer is a variable used to store an address, which uniquely identifies a piece of memory space.

2. The pointer size is fixed at 4/8 bytes (32-bit platform/64-bit platform).

3. The pointer has a type, and the type of the pointer determines the step size of the pointer +- integer, and the authority of the pointer dereference operation.

4. Arithmetic with pointers.

In this chapter, we continue to explore the advanced topic of pointers.

character pointer

In the pointer type, we know that there is a pointer type that is a character pointer char*;

General use:

Code const char* p = "abcdef" ; It is easy for students to think that the string abcdef is put in the character pointer p, but the essence is to put the address of the first character of the string abcdef in p.

 The above code means to store the address of the first character a of a constant string in the pointer variable p.

Here is a written test question:

#include <stdio.h>
int main()
{
	char str1[] = "hello world.";
	char str2[] = "hello world.";
	const char* str3 = "hello world.";
	const char* str4 = "hello world.";
	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;
}

The final output here is:

 reason:

 Here str3 and str4 point to the same constant string. C/C++ will store constant strings in a separate memory area. When several pointers point to the same string, they will actually point to the same block of memory. But when using the same constant string to initialize different arrays, different memory blocks will be opened up. So str1 and str2 are different, and str3 and str4 are the same.

array of pointers

In the chapter "Pointers", we also learned about pointer arrays, which are an array of pointers. Here we review again, what does the following pointer array mean?

You can use an analogy to remember:

  • Array of Integers - an array that stores integers
  • character array - an array of characters
  • array of pointers - an array of pointers

 A two-dimensional array is simulated using an array of pointers:

int main()
{
	int arr1[] = { 1,2,3,4,5 };
	int arr2[] = { 2,3,4,5,6 };
	int arr3[] = { 3,4,5,6,7 };

	int* arr[] = { arr1,arr2,arr3 };

	int i = 0, j = 0;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 5; j++)
		{
			printf("%d ", arr[i][j]);
			//printf("%d ", *(arr[i] + j));
			//printf("%d ", *(*(arr + i) + j));
			//三种方法打印的效果相同
		}
		printf("\n");
	}

	return 0;
}

  Print result:

array pointer

Definition of array pointer

Array pointers are pointers? Or an array? The answer is: pointers.

We are already familiar with:

  • Integer pointer—a pointer to an integer variable, a pointer variable that stores the address of an integer variable.
  • Character pointer—a pointer to a character variable, a pointer variable that stores the address of a character variable.
  • Array pointer - a pointer to an array, which stores the address of the array.

Which of the following codes is an array pointer?

 int (*p)[10] is an array pointer.

& array name VS array name

Understanding of array names:

The array name is the address of the first element of the array:

  Although the array name is the address of the first element of the array, there are two exceptions:

1. sizeof (array) name, the array name here is not the address of the first element of the array, the array name here represents the entire array, sizeof (array) name calculates the size of the entire array, and the unit is byte.

2.&Array name, the array here still represents the entire array, and &array name retrieves the address of the entire array.

   Otherwise, all array names elsewhere are the address of the first element of the array.

In order to let everyone see the difference between arr, &arr[0], &arr in the above code, we can use the following method:

1. When debugging in the VS compiler, use the watch window to observe the type:

  2. The step size of different types of pointer plus 1 movement will be different:

 For array names, & array name gets the address of the entire array

Use of array pointers

 How is the array pointer used? Since the array pointer points to an array, the address of the array should be stored in the array pointer. Look at the code:

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int(*p)[10] = &arr;  //*&arr==arr
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", *((*p) + i));
		//printf("%d ", (*p)[i]); 两种方法都可以打印数组
	}
	return 0;
}

In practical applications, we rarely use this method to write code. So how to try to use the array pointer? It is generally convenient on two-dimensional arrays.

When we usually call a function using a two-dimensional array to pass parameters, we generally use the form of a pointer as a formal parameter:

//常规方法——二维数组传参,形参是二维数组
void Print(int arr[3][5], int r, int c)
{
	int i = 0, j = 0;
	for (i = 0; i < r; i++)
	{
		for (j = 0; j < c; j++)
		{
			printf("%d ", arr[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 };
	Print(arr, 3, 5);//常规方法
	return 0;
}

Today, we can pass parameters in the form of pointers:

//二维数组传参——形参是指针的形式
void Print2(int(*p)[5], int r, int c)
{
	int i = 0, j = 0;
	for (i = 0; i < r; i++)
	{
		for (j = 0; j < c; j++)
		{
			printf("%d ", *(*(p + i) + 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 };
	//Print1(arr, 3, 5);//常规方法
	Print2(arr, 3, 5);
	return 0;
}

Summary: One-dimensional array parameter passing, the formal parameter part can be an array or a pointer.

//形参是数组的形式
void test1(int arr[],int sz)
{}
//形参是指针的形式
void test2(int *p,int sz)
{}
int main()
{
	int arr[5] = { 0 };
	test1(arr, 5);
	test2(arr, 5);
	return 0;
}

Two-dimensional array parameter passing, the formal parameter part can be an array or a pointer

//形参是数组
void test3(char arr[3][5],int r,int c)
{}
//形参是指针
void test4(char(*p)[5], int r, int c)
{}
int main()
{
	char arr[3][5] = { 0 };
	test3(arr, 3, 5);
	test4(arr, 3, 5);
	return 0;
}

 

Array parameters and pointer parameters

When writing code, it is inevitable to pass [array] or [pointer] to the function. How should the parameters of the function be designed?

One-dimensional array parameter passing

Determine whether the following functions are designed correctly:

#include <stdio.h>
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?
{}
int main()
{
	int arr[10] = { 0 };
	int* arr2[20] = { 0 };
	test(arr);
	test2(arr2);
}

 

Two-dimensional array parameter passing

Determine whether the following functions are designed correctly:

void test(int arr[3][5])//ok?
{}
void test(int arr[][])//ok?
{}
void test(int arr[][5])//ok?
{}
void test(int* arr)//ok?
{}
void test(int* arr[5])//ok?
{}
void test(int(*arr)[5])//ok?
{}
void test(int** arr)//ok?
{}
int main()
{
	int arr[3][5] = { 0 };
	test(arr);
}

Summary: When passing parameters in a two-dimensional array, the design of function parameters can only omit the first [] number.
Because for a two-dimensional array, you don't need to know how many rows there are, but you must know how many elements are in one row. This is convenient for operation.

First-level pointer parameter passing

 Thinking: When the parameter part of a function is a first-level pointer, what parameters can the function receive?

Secondary pointer parameter passing

 Print result:

 Thinking: When the parameter of the function is a secondary pointer, what parameters can it receive?

 

Today’s article will end here first, and the rest of this chapter will continue to be updated~~

Guess you like

Origin blog.csdn.net/m0_73648729/article/details/131817671