Difficulties in playing with pointers (1)

1. Character pointer

1. Two ways to write character pointers

int main()
{
    
    
	char ch = 'a';
	char*pc = &ch;
	char*p = "abcdef";
	//将首字符a的地址放入p中
	return 0;
}

You can use address symbols or double quotes and characters. Here abcdef is a constant string and cannot be changed.

Two, pointer array

Simply put, it is an array of pointers.

int main()
{
    
    
	int *arr[10];
	char*ch[20];
	return 0;
}

Three, array pointer

Essentially a pointer

int main()
{
    
    
	int a = 10;
	int *p = &a;
	char ch = 'w';
	char*pc = &ch;
	int arr[10] = {
    
     0 };
	int(*pa)[10] = &arr;
	//pa就是一个指向数组的指针
	//&arr数组名是整个数组,取出的是数组的地址
	return 0;
}

Application of array pointer
eg: printing two-dimensional array
1. Array method

void print(int arr[3][5], int r, int c)
{
    
    
	int i = 0;
	for (i = 0; i < r; i++)
	{
    
    
		int j = 0;
		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 } };
	printf(arr, 3, 5);
	return 0;
}

2. Array pointer method

void print(int (*p)[5],int r,int c)
{
    
    
	int i = 0;
	for (i = 0; i < r; i++)
	{
    
    
		int j = 0;
		for (j = 0; j < c; j++)
		{
    
    
			printf("%d ", *(*(p + i) + j));
		}
	}
}
int main()
{
    
    
	int arr[3][5] = {
    
     {
    
     1, 2, 3, 4, 5, }, {
    
     2, 3, 4, 5, 6 }, {
    
     3, 4, 5, 6, 7 } };
	//二维数组传参,数组名也是首元素地址,二维数组的首元素是第一行的地址
	//传过去的是第一行的地址
	printf(arr, 3, 5);
	return 0;
}

Analogous to our one-dimensional array parameter transfer, the first address is passed, and it is passed with a one-dimensional pointer or array to receive it, and a two-digit array is passed as the parameter. The address passed is the address of the first row, which is received with an array pointer.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_53831496/article/details/113746347