I just start learning C++. I thought I can access an integer array by using pointer arithmetic method. why does the first program work but the second one does not???
#include<iostream>
using namespace std;
int main()
{
int array [5], i, *p;
for (i = 0; i < 5; i++)
array [i] = i;
for (i = 0; i <5; i++)
cout << array [i] <<' ';
cout << "\n";
p = array;
cout << *p << ' ' << *(p+1) << ' ' << *(p+2) << ' ' << *(p+3) << ' '
<< *(p + 4);
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int array [5], i, *p;
for (i = 0; i < 5; i++)
array [i] = i;
for (i = 0; i <5; i++)
cout << array [i] <<' ';
cout << "\n";
p = array;
for (i = 0; i < 5; i++)
{
*p = *p * *p * *p;
p++;
}
for (i = 0; i <5; i++)
cout << array [i] << ' ' ;
cout << "\n";
cout << *p << ' ' << *(p+1) << ' ' << *(p+2) << ' ' << *(p+3) << ' ' << *(p + 4);
return 0;
}