You've got a 3-element array. Why are you trying to manipulate 4 elements?
#include <iostream>
int main()
{
int array[] = {1,2,3}, *ptr = array;
for ( std::size_t i = 0; i < sizeof array / sizeof *array; ++i )
{
std::cout << "array [ " << i << " ] = " << array[i] << std::endl;
}
for ( std::size_t i = 0; i < sizeof array / sizeof *array; ++i )
{
std::cout << "*(ptr + " << i << ") = " << *(ptr + i) << std::endl;
}
return 0;
}
/* my output
array [ 0 ] = 1
array [ 1 ] = 2
array [ 2 ] = 3
*(ptr + 0) = 1
*(ptr + 1) = 2
*(ptr + 2) = 3
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>hey ..d ans is very simple d array shld be declare as array[4] nt as array[3] ..dis is solve it
Not only are your grammar and spelling atrocious, this thread is over two years old. Look at the date of the thread before you post in it, please. And keep in mind that not all of our members speak English fluently, and your silly abbreviations could be terribly confusing.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
The question was already answered in this old, and now closed, thread.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314