A pointer array is an array that stores pointers
analogy:
Character array --an array that stores characters
char arr1[10]; // 字符数组
Integer array --an array that stores integers
int arr2[10]; // 整型数组
Pointer array --stores pointers
char* arr3[10];// 存放字符指针的数组
int* arr4[10]; // 存放整型指针的数组
int main()
{
char* arr[3] = { "abc","bcd","cde" };
for (int i = 0; i < 3; i++)
{
printf("%c ", *(arr[i]));
}
printf("\n");
for (int i = 0; i < 3; i++)
{
printf("%s\n", arr[i]);
}
return 0;
}
arr is an array of character pointers, and its element arr[i] points to the first element of the string. The first character can be accessed by dereferencing *(arr[i]), and arr[i] is printed in %s format, that is, the string pointed to by arr[i] is printed.
The execution result of the above program:
Simulate a two-dimensional array 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[3] = { arr1,arr2,arr3 }; // 整型指针数组
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
printf("%d ", arr[i][j]);// 等价*(arr[i]+j)
}
printf("\n");
}
return 0;
}
Results of the:
Continue to learn advanced pointer content, see the next article: Array Pointers