
- Forum
- Programming Talk
- C and C++
- Garbage Value Output
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 j; for(j=0;j<2;j++) { ...
-
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?
-
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.

Reply With Quote





