C++ primer(第五版) 练习 3.35
题目:编写一段程序,利用指针将数组中的元素置为0。
答:
#include <iostream>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::begin;
using std::end;
int main()
{
int a[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 };
int *b = begin(a);
int *e = end(a);
cout << "数组a中原来的元素的值为:" << endl;
for (auto b : a)
cout << b << " ";
cout << endl;
while (b != e)
{
*b = 0;
b++;
}
cout << "数组a修改后的元素的值为:" << endl;
for (auto c : a)
cout << c << " ";
cout << endl;
return 0;
}
执行结果: