Why is the last element not 7

``````int array[3];
int *ary;

ary=array;
array[0]=0;
array[1]=0;
array[2]=0;
array[3]=44;
for (int i=0;i<4;i++)
{
cout<<array[i];
}
cout<<endl;

*(ary+0)=1;
*(ary+1)=0;
*(ary+2)=0;
*(ary+3)=7;
for (int i=0;i<4;i++)
{
cout<<array[i];
}
cout<<endl;``````

Output
00044
1003<-----------------Why 3 and not 7!!!!!!

## All 7 Replies

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
*/``````

Oh, my poor child :-),
[just kidding].
If you have an array like array[3] you can use only 3 elements i.e
1. array[0]
2. array[1]
3. array[2]

If you go beyound that you will get errors as you would be writing over memory used by some other program.

so array[3] is the fouth element.so declare it as int array[4].Should solve you prob

Wow. I completely missed that. That was a typo. I am bad. I got so focused on why my ouput was wrong and looked at my pointer etc and completely ignored the array declaration. HAHAHAHAHA. Oh well!!!! One note, apparently Borland does not optimize your code as Visual C++ because I would get the correct output on Visual C++ and not Borland Builder 6.

Thanks.

Why is the last element not 7

``````int array[3];
int *ary;

ary=array;
array[0]=0;
array[1]=0;
array[2]=0;
array[3]=44;
for (int i=0;i<4;i++)
{
cout<<array[i];
}
cout<<endl;

*(ary+0)=1;
*(ary+1)=0;
*(ary+2)=0;
*(ary+3)=7;
for (int i=0;i<4;i++)
{
cout<<array[i];
}
cout<<endl;``````

Output
00044
1003<-----------------Why 3 and not 7!!!!!!

hey ..d ans is very simple d array shld be declare as array[4] nt as array[3] ..dis is solve it

commented: Bad English, Unnecessary Reply for an year old thread. You need to learn forum etiquette. +0

>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.

Why is the last element not 7

``````int array[3];
int *ary;

ary=array;
array[0]=0;
array[1]=0;
array[2]=0;
array[3]=44;
for (int i=0;i<4;i++)
{
cout<<array[i];
}
cout<<endl;

*(ary+0)=1;
*(ary+1)=0;
*(ary+2)=0;
*(ary+3)=7;
for (int i=0;i<4;i++)
{
cout<<array[i];
}
cout<<endl;``````

Output
00044
1003<-----------------Why 3 and not 7!!!!!!

hi man.i have tried this,the output comes out to be ooo44,1007 not 1004 u have to try this again and see.i think its problem of ur compiler.but as i think it is right as 1007

commented: Quit digging up old threads you fool. +0