Home >Common Problem >What is the difference between array pointer and array of pointers

What is the difference between array pointer and array of pointers

zbt
zbtOriginal
2023-09-22 09:51:502091browse

The differences between array pointers and pointer arrays are: 1. The array pointer is a pointer, while the stored pointer array is an array; 2. The declaration method of the array pointer is int *p = arr;, while the declaration of the pointer array is The method is int *arr[5];; 3. The array pointer can access the elements in the array in the form of p[i], while the pointer array needs to access the elements in the array in the form of arr[i].

What is the difference between array pointer and array of pointers

Array pointer and pointer array are two different representations of pointers in C language. They can be converted to each other in some cases, but in essence they are are different data types.

An array pointer is a pointer that points to the first element of an array. Array pointers can be used to access and operate elements in an array, and are equivalent to the address of an array. The declaration and use of array pointers are as follows:

int arr[5]; // 声明一个整型数组
int *p = arr; // 声明一个数组指针,指向数组 arr 的第一个元素
p[0] = 1; // 通过数组指针访问数组的第一个元素

A pointer array is an array, and each element of it is a pointer. A pointer array can be used to store multiple pointers, which is equivalent to an array of pointers. Arrays of pointers are declared and used as follows:

int *arr[5]; // 声明一个指针数组,数组元素类型为整型指针
arr[0] = &p[0]; // 声明一个整型指针,指向数组 arr 的第一个元素
arr[1] = &p[1]; // 声明一个整型指针,指向数组 arr 的第二个元素

The main difference between array pointers and arrays of pointers lies in their different natures. An array pointer is a pointer that points to the first element of an array, equivalent to the address of an array. A pointer array is an array, and each element of it is a pointer, which is equivalent to an array of pointers.

In addition, there are some differences in the declaration and use of array pointers and pointer arrays. The declaration method of array pointer is int *p = arr;, and the declaration method of pointer array is int *arr[5];. In terms of usage, array pointers can access elements in the array in the form of p[i], while pointer arrays need to access elements in the array in the form of arr[i].

Although array pointers and pointer arrays can be converted to each other in some cases, they are essentially different. An array pointer is a pointer, which points to the first element of an array; a pointer array is an array, and each element of it is a pointer.

The above is the detailed content of What is the difference between array pointer and array of pointers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn