Detailed explanation of the advanced part of C language (advanced pointers 1)

Hello everyone! I have already written the basic content of pointers, you can move to my article: Detailed explanation of the advanced part of C language (Basic pointers)_In short, it is a very boring blog-CSDN Blog

 I won’t go into details about the basic content and will take you directly to the advanced content:


Table of contents

1.Character pointer

1.Explain 

2.Examples 

2. Pointer array 

1.Explain 

2. Practice

3. Array pointer

1. Definition of array pointer 

2. Syntactic comparison between array pointers and pointer arrays 

3.&array name VS array name 

4. Use of array pointers 

 4. Array parameters and pointer parameters

1. One-dimensional array parameter transfer 

 2. Passing parameters from two-dimensional array

 3. First-level pointer parameter passing

4. Secondary pointer parameter passing 

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


1.Character pointer

1.Explain 

Among the types of pointers, we know that there is a pointer type called character pointer char*; 

Its general usage scenario is: like this

#include<stdio.h>
int main()
{
	char a = 'z';
	char* pa = &a;
	return 0;
}

 And this:

int main()
{
	char* arr = "hello";
	printf("%s", arr);
	return 0;
}

 Regarding the statement char* arr = "hello";, it is particularly easy for you to think that the string hello is placed in the character pointer arr, but the essence is that the address of the first character of the string hello is placed in arr.

2.Examples 

Let’s look at an example question to deepen our understanding: You can think about it first and then compare the answers.

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

Answer:

 Are you guys right?

  • When using the "==" operator to compare pointers, what is actually compared is whether the addresses pointed to by the pointers are the same, rather than whether the contents of the strings are the same.
  • Here str3 and str4 point to the same constant string. C/C++ will store constant strings in a separate memory area as several pointers. When pointing to the same string, they will actually point to the same memory.
  • When using the same constant string to initialize different arrays, different memory blocks will be allocated. So str1 is different from str2, str3 is different from str4

2. Pointer array
 

1.Explain 

  • An array of pointers is an array in which each element is a pointer.
  • It can store multiple pointers, each pointer can point to a different type of data.
  • The syntax for declaring a pointer array is: type *ptr[size] , where type is the data type pointed to by the pointer and size is the size of the array.

Example: int num1 = 10, num2 = 20, num3 = 30;

           int *ptr[3] = {&num1, &num2, &num3};

           //Declare an array containing 3 integer pointers 

2. Practice

Use an array of pointers to simulate a two-dimensional array

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

	int* pa[3] = { &arr1,&arr2,&arr3 };

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			printf("%d ", pa[i][j]);
		}
		printf("\n");
	}
	return 0;
}

 


3. Array pointer


1. Definition of array pointer
 

  • An array pointer is a pointer variable that points to an array.
  • It can point to the first element of an array or the entire array.
  • The syntax for declaring an array pointer is: type (*ptr)[size] , where type is the type of the array element and size is the size of the array.

Example: int arr[5] = {1, 2, 3, 4, 5};

           int (*ptr)[5] = &arr; // Declare a pointer to an array containing 5 integers

2. Syntactic comparison between array pointers and pointer arrays 

int *p1[10];
int (*p2)[10];

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

int *p1[10]; is an array of pointers

//Explanation: p is first combined with [], indicating that p is an array, and then the type of the array is int*

3.&array name VS array name
 

  •  &arr and arr, although their values ​​are the same, their meanings should be different.
  •  &arr represents the address of the array, not the address of the first element of the array, the address of the array + 1, skipping the size of the entire array
  • The array name is the address of the first element of the array, with two exceptions:

1.sizeof(array name), the array name here represents the entire array, sizeof(array name) calculates the size of the entire array, the unit is bytes.

2.&Array name, the array name here represents the entire array, and the address of the array is taken out.

 

4. Use of array pointers 

void print_arr(int(*arr)[5], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			printf("%d ", arr[i][j]);
		}
		printf("\n");
	}
}
int main()
{
	int arr[3][5] = { 1,2,3,4,5,6,7,8,9,10 };
	//数组名arr,表示首元素的地址
	//但是二维数组的首元素是二维数组的第一行
	//所以这里传递的arr,其实相当于第一行的地址,是一维数组(有五个元素)的地址
	//可以数组指针来接收
	print_arr(arr, 3, 5);
	return 0;
}
  •  int arr[5]; arr is an array that can store 5 integer data
  • int *parr1[10]; parr1 is an array. The array has 10 elements. The type of each element is int*.
  • int (*parr2)[10]; parr2 is a pointer, which points to a numerical value. The array pointed to has 10 elements, and the type of each element is int.
  • int (*parr3[10])[5]; parr3 is an array, which is an array that stores array pointers. This array has 10 elements. The array pointed to by the stored array pointer has 5 elements. Each element is of type int. .

Understanding: parr3[10] can be seen to be an array, and int (*)[5] is an array pointer type, so overall it is an array that stores the array pointer type.


 4. Array parameters and pointer parameters

1. One-dimensional array parameter transfer
 

  •  Array parameter transfer, formal parameters can be written in array form
  •  The essence of array parameter passing is to pass the address of the first element of the array. The formal parameter can also be a pointer.
void test(int arr[])//可以    数组传参,形参是可以写成数组形式的
{}
void test(int arr[10])//可以
{}
void test(int* arr)//可以     数组传参的本质是传递了数组首元素的地址,形参也可以是指针
{}
void test2(int* arr[20])//可以 
{}
void test2(int** arr)//可以  传过来的是int* 的地址,就用int**来接收
{}
int main()
{
	int arr[10] = { 0 };

	int* arr2[20] = { 0 };
	test(arr);
	test2(arr2);
}

 2. Passing parameters from two-dimensional array

 The formal parameter is a two-dimensional array or array pointer

//总结:二维数组传参,函数形参的设计只能省略第一个[]的数字。
//因为对一个二维数组,可以不知道有多少行,但是必须知道一行多少元素。
//这样才方便运算。

void test(int arr[3][5])//可以
{}
void test(int arr[][])//不可以:只能省略行,不能省略列
{}
void test(int arr[][5])//可以
{}
									
void test(int* arr)//不可以
{}
void test(int* arr[5])//不可以
{}
void test(int(*arr)[5])//可以
{}
void test(int** arr)//不可以
{}
int main()
{
	int arr[3][5] = { 0 };
	test(arr);
}

 3. First-level pointer parameter passing

 Just write the formal parameter part as a first-level pointer.

void print(int* p, int sz)  //形参的部分写成一级指针就行了
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d\n", *(p + i));
	}
}
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9 };
	int* pa = arr;
	int sz = sizeof(arr) / sizeof(arr[0]);
	//一级指针p,传给函数
	print(pa, sz);
	return 0;
}

4. Secondary pointer parameter passing
 

Just use the secondary pointer to receive 

 

void test(int** ptr)
{
	printf("num = %d\n", **ptr);
}
int main()
{
	int n = 10;
	int* p = &n;
	int** pp = &p;
	test(pp);//传过来的是二级指针
	test(&p);
	return 0;
}

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

As long as it is the address of a first-level pointer:

int a=10;

int* p=&a;

int** pp=&p;

test ( &p ); test ( pp ) ;

int* arr[10]; Pointer array, the first element is a first-level pointer, the address of the first element is passed

test(arr); 


This time the content sorting is done here. I will speed up the update of subsequent content. Thank you for your support! !

 

 

 

Guess you like

Origin blog.csdn.net/qq_74415153/article/details/133037969