Exforsys
+ Reply to Thread
Results 1 to 2 of 2

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++) { ...

  1. #1
    Ralph is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    114

    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?


  2. #2
    ashlee is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    108
    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.


Latest Article

Network Security Risk Assessment and Measurement

Read More...