Create a program that declares an integer array with five elements. Now store in that integer array five different elements (10, 20, 30, 40, 50) using a pointer variable only.
Hints
The identifier of an array is equivalent to the address of its first element, as a pointer is equivalent to the address of the first element that it points to.
If A [5] is an array then the expressions A[2] and *(A+2) designate the same thing. Use (*A+2) to store the value in the third element of array.
Solution
#include <iostream.h>
main()
{
int A[5]= {10, 20, 30, 40, 50};
int i;
int * Aptr = A;
for( i=0; i<5; i++)
{
cout << "(*A+" << i << ")=" << A[i] << '\n';
}
}
is it correct???????? ???????????
Hints
The identifier of an array is equivalent to the address of its first element, as a pointer is equivalent to the address of the first element that it points to.
If A [5] is an array then the expressions A[2] and *(A+2) designate the same thing. Use (*A+2) to store the value in the third element of array.
Solution
#include <iostream.h>
main()
{
int A[5]= {10, 20, 30, 40, 50};
int i;
int * Aptr = A;
for( i=0; i<5; i++)
{
cout << "(*A+" << i << ")=" << A[i] << '\n';
}
}
is it correct???????? ???????????
Comment