954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Why am I getting this output

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

TJW
Newbie Poster
12 posts since May 2004
Reputation Points: 11
Solved Threads: 0
 

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
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

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.

TJW
Newbie Poster
12 posts since May 2004
Reputation Points: 11
Solved Threads: 0
 

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

joshilay
Light Poster
31 posts since Jul 2006
Reputation Points: 10
Solved Threads: 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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

sagar arora
Newbie Poster
4 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

The question was already answered in this old, and now closed, thread.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You