Advanced C language pointer (1)

Table of contents

1. Character pointer

2. Array of pointers

 Understanding Pointer Arrays by Analogy

2.1 General form of pointer array

2.2 Pointer array simulation to realize two-dimensional array

3. Array pointer

 Understanding array pointers by analogy

3.1 General form of array pointer

3.2& Array Name VS Array Name

3.3 Use of array pointers

Four, array parameters, pointer parameters

4.1 One-dimensional array parameter passing

4.2 Two-dimensional array parameter passing

4.3 Level 1 Pointer Parameter Passing 

4.4 Second-level pointer parameter passing


Preface: In the initial pointer, we learned about the concept of pointers. A pointer is a variable used to store an address. The address uniquely identifies a piece of memory space. At the same time, the pointer has a type. The type of the pointer determines the step size of the +-integer of the pointer and the authority of the pointer dereference operation. Next, let me lead you to understand various types of pointers.

1. Character pointer

The general form of a character pointer:

       char* variable name

 General use of character pointers

int main()
{
	char ch = 'w';
	char* pc = &ch;       //pc就是字符指针

    ch='a';
	*pc = 'a';

	return 0;
}

The value of the variable ch can be changed directly, or the value of the variable ch can be changed through a pointer.

int main()
{
    char arr[]="abcdef";
    //创建一个数组,用字符串来初始化这个数组
    const char* p="abcdef";  //常量字符串
    //本质是将字符串首元素的地址赋给指针变量p
}

The essence is to assign the address of the first element of the string to the pointer variable p. Because it is a constant string, it cannot be modified, so add const to prevent *p from modifying the string.

Verify by printing that the address of the first element of the string is stored in p

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

	printf("%s\n", p);
	printf("%c\n", *p);
}

 Example analysis:

int main()
{
	char str1[] = "hello bit.";
	char str2[] = "hello bit.";

	const char* str3 = "hello bit.";
	const 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;
}

 str1 and str2 are two created arrays, each with its own independent space. Although the two arrays store the same string, their addresses are different. str1 and str2 are array names, and the array name represents the address of the first element, so str1! =str2

2. Array of pointers

 Understanding Pointer Arrays by Analogy

Integer array -- an array that stores integers

character array -- a number to store characters

array of pointers - an array of pointers

2.1 General form of pointer array

        int* arr1[10]; // integer pointer array

        char* arr2[10]; //Array of character pointers

2.2 Pointer array simulation to realize two-dimensional array

#include <stdio.h>
int main()
{
	int arr1[] = { 1,2,3,4,5 };		//arr1 - 数组名 - 首元素地址 int*
	int arr2[] = { 2,3,4,5,6 };		//arr2 - 数组名 - 首元素地址 int*
	int arr3[] = { 3,4,5,6,7 };		//arr3 - 数组名 - 首元素地址 int*

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

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

 

The three created arrays are not contiguous in memory space, so they are not really two-dimensional arrays.

3. Array pointer

 Understanding array pointers by analogy

Integer Pointer - A pointer to an integer variable. That is, a pointer variable that stores the address of an integer variable

Character Pointer -- A pointer to a character variable. That is, a pointer variable that stores the address of a character variable

Array Pointer -- A pointer to an array variable. That is, the pointer variable that stores the address of the array variable

3.1 General form of array pointer

        int (*p)[10]; 

Explanation: p is first combined with *, indicating that p is a pointer variable, and then points to an array of 10 integers (the type pointed to is int [10]). So p is a pointer pointing to an array, called an array pointer.

Note: The priority of [] is higher than that of *, so () must be added to ensure that p is combined with * first.

3.2& Array Name VS Array Name

The array name is usually the address of the first element, with two exceptions

1. sizeof(array name) The array name is placed inside sizeof() alone, where the array name represents the entire array, and the calculation is the size of the entire array

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

3.3 Use of array pointers

int main()
{
     int arr[10] = {1,2,3,4,5,6,7,8,9,0};
     int (*p)[10] = &arr;    //把数组arr的地址赋值给数组指针变量p
     return 0;
}

void Print(int(*arr)[5], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf("%d ", arr[i][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;
}

Code interpretation:

printf("%d ", *(*(arr+i)+j));

*&arr==arr first element address

*(arr+i)==arr+i is equivalent to the first element address of row i, +j skips j elements  

Each row of a two-dimensional array can be understood as an element of a two-dimensional array, and each row is a one-dimensional array. 

The array name arr indicates the address of the first element. The first element of the two-dimensional array is the first row of the two-dimensional array, so the arr passed here is actually equivalent to the address of the first row, which is the address of the one-dimensional array and can be received by the array pointer. .

Replenish:

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

void test1(int arr[5], int sz)
{}

void test2(int* arr, int sz)
{}

int main()
{
	int arr[5] = { 0 };
	test1(arr, 5);
	test2(arr, 5);
	return 0;
}

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

void test1(int arr[3][5], int r,int c)
{}

void test2(int (*p)[5], int r,int c)
{}

int main()
{
	int arr[3][5] = { 0 };
	test1(arr,3, 5);
	test2(arr,3, 5);
	return 0;
}

Note: The formal parameters are written in the form of an array to make it easier to understand, and they are essentially pointers.

Four, array parameters, pointer parameters

4.1 One-dimensional array parameter passing

void test(int arr[10])//ok?
{}

void test(int *arr)//ok?
{}

int main()
{
     int arr[10] = {0};
     test(arr);
}

The first one is correct, the array parameter is received by the array, but the essence of the formal parameter is a pointer. The number of elements can be omitted.

The second one is correct, the array name passes the address of the first element, so it can be received by a pointer.

void test(int* arr[20])//ok?
{}

void test(int **arr)//ok?
{}

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

The first one is correct, an array of pointers can be received with an array of pointers.

The second one is correct, the type of the variable is an array of pointers, the address is stored in the array, the type of each element is int*, the name of the array is the address of the first element, and the actual parameter is the address of the pointer, so use the second level The pointer is received.

4.2 Two-dimensional array parameter passing

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

 The first one is correct, the array pass parameters are received by numbers.

Note: For two-dimensional array parameter passing, the design of the function parameter 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.

 The fourth one is wrong. The array name of the two-dimensional array is the address of one line, and the type is int(*)[], which cannot be received by int* arr.

The sixth is wrong, the address of the integer variable cannot be received by the secondary pointer.

4.3 Level 1 Pointer Parameter Passing 

       When the parameter part of a function is a first-level pointer, the parameters that can be passed by the actual parameter of the function

void test(int* p)
{}

int main()
{
	int n = 10;
	test(&n);

	int* p = &n;
	test(p);

	int arr[5] = { 0 };
	test(arr);
	return 0;
}

4.4 Second-level pointer parameter passing

          When the parameter part of a function is a secondary pointer, the parameters that can be passed by the actual parameter of the function

void test(int** p)
{}

int main()
{
	int n = 10;
    int* p=&n;
	test(&p);

	int** pp = &p;
	test(pp);

	int* arr[5] = { 0 };
	test(arr);
	return 0;
}

The content of this time is over here. I hope you can gain something after reading it, and thank you readers for your support. If you have any questions about the article, you can leave a message in the comment area. The blogger must carefully revise it and write better articles in the future.  

Guess you like

Origin blog.csdn.net/2301_76207836/article/details/131613324