Exforsys

Online Training

Garbage Value Output

This is a discussion on Garbage Value Output within the C and C++ forums, part of the Programming Talk category; I have my C program as below: void main() { int testarray[2]={1,2}; int *ar = &testarray[-1]; int ...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-02-2007, 04:33 PM
Senior Member
 
Join Date: Apr 2006
Posts: 139
Ralph is on a distinguished road
Question Garbage Value Output

I have my C program as below:

void main()
{
int testarray[2]={1,2};
int *ar = &testarray[-1];
int j;
for(j=0;j<2;j++)
{
printf("\t%d",*ar);
ar++;
}
}

When I run the above program it gives some garbage value. Can anyone tell me why I get garbage value as output for the above program?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-07-2007, 04:33 AM
Senior Member
 
Join Date: Apr 2006
Posts: 125
ashlee is on a distinguished road
You get some garbage value as output because the array in C programming starts at index 0 and in the statement
int *ar = &testarray[-1];

The address of testarray[-1] is stored in the ar and the contents when it is printed in the output statement gives a garbage value.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
C Programming - Managing Input and Output Operations JobHelper Career Advice 0 04-15-2006 08:30 AM
Controlling PHP Output: Caching and compressing dynamic pages techguru Unix Articles and Tutorials 0 08-28-2005 11:26 AM
Aditi Technologies C Test JobHelper Placement Papers 0 05-04-2005 09:03 AM
Solaris x86 FAQ 2/2 Dan Anderson Tech FAQ 0 04-27-2004 08:24 PM
[gnu.bash.bug] BASH Frequently-Asked Questions (FAQ version 3.26) Chet Ramey Tech FAQ 0 04-26-2004 11:00 AM


All times are GMT -4. The time now is 11:32 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.