English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about the relationship between arrays and pointers, and use them effectively in programs.
PointersIs a variable that stores the address. A pointer can not only store the address of a single variable, but also storeArrayThe address of the unit.
See the following example:
int* ptr; int a[5] ptr = &a[2] // &a[2Is a[5The address of the third element is].
Assuming the pointer needs to point to the fourth element of the array, that is, the address of the fourth array element in the above case.
Since ptr points to the third element in the above example, ptr + 1It points to the fourth element.
You might think that ptr +1It provides you with the address of the next byte of ptr. But this is incorrect.
This is because pointer ptr is a pointer to int, and the size of int is fixed for the operating system (64The size of the int type in the bit operating system is4bytes). Therefore, ptr and ptr +1will be4bytes.
If pointer ptr is a pointer to char, then the difference in addresses between ptr and ptr + 1The addresses between will differ by1bytes, because the size of a character is1bytes.
C ++The program uses arrays and pointers to display the addresses of array elements
#include <iostream> using namespace std; int main() { 5] float *ptr; cout << "Use array to display the address: " << endl; for (int i = 0; i < 5; ++i) { cout << "&arr[" << i << "] = " << &arr[i] << endl; // ptr = &arr[0] ptr = arr; cout << "\nUse pointer to display the address: " << endl; for (int i = 0; i < 5; ++i) { cout << "ptr + " << i << " = " << ptr + i << endl; return 0;
Output Result
Use array to display the address: &arr[0] = 0015F718 &arr[1] = 0015F71C &arr[2] = 0015F720 &arr[3] = 0015F724 &arr[4] = 0015F728 Use pointer to display the address: ptr + 0 = 0015F718 ptr + 1 = 0015F71C ptr + 2 = 0015F720 ptr + 3 = 0015F724 ptr + 4 = 0015F728
In the above program, different pointers ptr are used to display the address of array element arr.
However, array elements can be accessed using pointer notation with the same array name arr. For example:
int arr[3] &arr[0] is equivalent to arr &arr[1] is equivalent to arr + 1 &arr[2] is equivalent to arr + 2
C ++The program uses pointer symbols to display the addresses of array elements.
#include <iostream> using namespace std; int main() { 5] cout << "Use pointer symbols to display the address: " << endl; for (int i = 0; i < 5; ++i) { cout << arr + i << endl; return 0;
Output Result
Use pointer symbols to display the address: 045FB90 045FB94 045FB98 045FB9C 045FBA0
You know that the pointer ptr stores the address, and the expression*ptr to give the value stored at the address.
Similarly, you can use*(ptr + 1) to get the value stored at the address of pointer ptr + 1of the values.
Consider the following code:
int ptr[5] = {3, 4, 5, 5, 3};
&ptr[0] is equal to ptr ,*ptr is equal to ptr[0]
&ptr[1] is equal to ptr + 1,*(ptr + 1) is equal to ptr[1]
&ptr[2] is equal to ptr + 2,*(ptr + 2) is equal to ptr[2]
&ptr[i] is equal to ptr + i ,*(ptr + i) is equal to ptr[i]
C ++A program to insert and display data entered using pointer symbols.
#include <iostream> using namespace std; int main() { 5] // Insert Data Using Pointer Symbols cout << "Input5Number of Digits: "; for (int i = 0; i < 5; ++i) { cin >> *(arr + i); // Display Data Using Pointer Symbols cout << "Display Data: " << endl; for (int i = 0; i < 5; ++i) { cout << *(arr + i) << endl; return 0;
Output Result
Input5Number of Digits: 125 145 180 225 335 Display Data: 125 145 180 225 335