Skip to content

Latest commit

 

History

History
56 lines (40 loc) · 1.24 KB

File metadata and controls

56 lines (40 loc) · 1.24 KB

C++ 程序:使用指针访问数组元素

原文: https://www.programiz.com/cpp-programming/examples/access-array-pointer

该程序声明了包含五个元素的数组,并使用指针访问了该数组的元素。

要理解此示例,您应该了解以下 C++ 编程主题:


示例:使用指针访问数组元素

#include <iostream>
using namespace std;

int main()
{
   int data[5];
   cout << "Enter elements: ";

   for(int i = 0; i < 5; ++i)
      cin >> data[i];

   cout << "You entered: ";
   for(int i = 0; i < 5; ++i)
      cout << endl << *(data + i);

   return 0;
} 

输出

Enter elements: 1
2
3
5
4
You entered: 1
2
3
5
4 

在该程序中,这五个元素由用户输入,并存储在整数数组data中。

然后,使用for循环访问data数组,并将数组中的每个元素打印到屏幕上。

访问此页面以了解指针与数组之间的关系