Advanced pointer (character pointer, pointer array, array pointer)

Posted by richclever on Sun, 27 Feb 2022 16:45:21 +0100

catalogue

1. Character pointer

1.1 method of use

1.2 testing

2. Pointer array

2.1 print string pointer array

2.2 printing integer pointer array

3. Array pointer

3.1 definition of array pointer

3.2 & array name VS array name

expand

3.3 use of array pointer

practice

Definition of pointer:

  1. A pointer is a variable used to store an address, which uniquely identifies a piece of memory space.
  2. The size of the pointer is fixed at 4 / 8 bytes (32-bit platform / 64 bit platform).
  3. Pointers are typed. The type of pointer determines the step size of the + - integer of the pointer and the permission of pointer dereference operation.
  4. Pointer operation.

When defining multiple variables in succession:

#include <stdio.h>
int main()
{
    int a, b;    //int a;  int b;
    int* pa, pb;    //int* pa;  int pb;
    int* pa, *pb;    //int* pa;  int* pb;
    return 0;
}

You can also use typedef or #define. Refer to: Comparison of typedef and #define differences

1. Character pointer

1.1 method of use

General use:

#include <stdio.h>
int main()
{
    char ch = 'w';
    char *pc = &ch;
    *pc = 'w';
    return 0;
}

There is another way to use it:

#include <stdio.h>
int main()
{
    const char* pstr = "hello bit.";    //Here, a string is stored in the read-only data area, and the content cannot be changed
    printf("%s\n", pstr);
    return 0;
}

1.2 testing

#include <stdio.h>
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 == str2\n");
    else
        printf("str1 != str2\n");
       
    if(str3 == str4)
        printf("str3 == str4\n");
    else
        printf("str3 != str4\n");
       
    return 0;
}

The running result is: STR1= str2    str3 == str4

The array name represents the address of its first element, and the other two pointers point to the address of the first element stored in the read-only area (the address is the same)

2. Pointer array

  • Pointer array: an array of pointers
  • Integer array: an array that holds integers
  • Character array: an array of characters

int* arr1[10]; / / array of integer pointers
char *arr2[4]; / / array of first level character pointers
char **arr3[5]; / / array of L2 character pointers

2.1 print string pointer array

#include <stdio.h>

int main()
{
	char* arr[] = { "abcdef", "qwer", "zhangsan" };
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	for (i = 0; i < sz; i++)
	{
		printf("%s ", arr[i]);    //Dereference is not required because the string is an array of char, that is, the nesting of the first element address
	}
	return 0;
}

2.2 printing integer pointer array

#include <stdio.h>

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;
	for (i = 0; i < 3; i++)
	{
		int j = 0;
		for (j = 0; j < 5; j++)
		{
			printf("%d ", arr[i][j]); //*(*(arr+i)+j)
		}
		printf("\n");
	}
	return 0;
}

3. Array pointer

3.1 definition of array pointer

Array pointer: pointer to an array

int* p1[10]; / / pointer array

int (*p2)[10]; / / array pointer (explanation: P is first combined with * to indicate that P is a pointer variable, and then points to an array of 10 integers. Therefore, P is a pointer to an array, which is called array pointer)

3.2 & array name VS array name

Analyze the difference between arr and & arr

#include <stdio.h>

int main()
{
	int arr[10] = { 0 };
	printf("%p\n", arr);
	printf("%p\n", arr+1);

	printf("%p\n", &(arr[0]));
	printf("%p\n", &(arr[0])+1);

	printf("%p\n", &arr);	//int(*p)[10] = &arr;
	printf("%p\n", &arr+1);

	return 0;
}

In fact: & arr represents the address of the array, not the address of the first element of the array.

In this example, the type of & arr is int(*)[10], which is an array pointer type

expand

#include <stdio.h>

int main()
{
	char arr[5];
	char(*pa)[5] = &arr;

	int* parr[6];
	int* (*pp)[6] = &parr;

	return 0;
}

At this time, the type of pp is int* (*)[6]

3.3 use of array pointer

Two dimensional array: arr[i] = = * (arr+i) = = p[i] = * (p+i)

#include <stdio.h>

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++)
		{
			//*(p+i) is equivalent to getting the i-th row of the two-dimensional array and the array name of the i-th row
			//The array name represents the address of the first element, which is also the address of the first element in line i
			printf("%d", *(*(p+i)+j));
            //              p[i][j]
            //p is the address of the first line
            //p+i is the address of line i
            //*(p+i) is the address of the first element in line i
            //*(* (p+i)+j) is the value of the first element in line i
		}
		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;
}

practice

int arr[5];
//arr is an integer array with five elements, each of which is of type int

int* parr1[10];
//Parr1 is an array with 10 elements. Each element is of type int *, so parr1 is an array of pointers

int (*parr2)[10];
//The combination of parr2 and * is a pointer of type int(*)[10], which points to an array. The array has 10 elements, and each element is of type int, so parr2 is an array pointer

int (*parr3[10])[5];
//The combination of parr3 and [] shows that parr3 is an array with 10 elements, and each element type of the array is int(*)[5] (an array pointer, which points to an array with 5 elements of type int)

After text transfer: ↓

int arr[5];
//arr is an integer array with five elements, each of which is of type int

int* parr1[10];
//Parr1 is an array with 10 elements. Each element is of type int *, so parr1 is an array of pointers

int (*parr2)[10];
//The combination of parr2 and * is a pointer of type int(*)[10], which points to an array. The array has 10 elements, and each element is of type int, so parr2 is an array pointer

int (*parr3[10])[5];
//The combination of parr3 and [] shows that parr3 is an array with 10 elements, and each element type of the array is int(*)[5] (an array pointer, which points to an array with 5 elements of type int)

Topics: C Back-end