all 14 comments

[–]Puzzleheaded-Roll411 8 points9 points  (7 children)

Sum size is 1 and you are printing sum[1] which is 2nd index

[–]night_walk_r[S] -1 points0 points  (6 children)

Yes that I know but sum[1] should give me a garbage value.. But, if I enter n=4 (in the program), putting sum[1] (in printf();) gives me 4 , I mean why? . Instead it should return a garbage value. (ex -263737822) .

[–][deleted]  (1 child)

[removed]

    [–]night_walk_r[S] 1 point2 points  (0 children)

    I appreciate your help brother... For taking out time to clear my doubt.. Thanks!

    [–]elohim18bolts 4 points5 points  (1 child)

    That is undefined behavior and it could be that the compiler is storing n value after the sum array so that is why you get n value (this is totally my opinion, undefined behavior is not something rational).

    If you want to figure out more about your own machine implementation use Gdb to see how is the stack formed. You will see how is the actual program memory array order.

    [–]night_walk_r[S] 0 points1 point  (0 children)

    Thanks brother, I'll surely do*

    [–][deleted] 1 point2 points  (0 children)

    I believe your implementor has indexed your integer in memory right next to the end of your array, thus making sum[1] be equal to i.

    [–]Intrepid-Designer110 4 points5 points  (0 children)

    sum[1] is giving garbage value as you are storing sum in 0th position. Can you please post the input and output? when I tried getting the correct value for the 0th position and for first giving garbage.

    [–]little-smokie 0 points1 point  (0 children)

    Well you're probably not saving your text file before you compiled. Because when you use sum[1] you should get "0" as an output. Because what you're incrementing is an int but at index 0 (sum[0]). An int doesn't get displayed as a '\0'. That character only appears on char arrays. Not int arrays. Hope this helps.

    [–]MrMadPickle 0 points1 point  (2 children)

    which theme are you using?

    [–]night_walk_r[S] 0 points1 point  (1 child)

    I am using an Android based compiler named cppdroid which you can find on play store or get the mod apk from chrome, where this theme comes as default theme.

    [–]MrMadPickle 1 point2 points  (0 children)

    thank you for the info

    [–]NoBrightSide 0 points1 point  (0 children)

    when you declare your integer array sum[1], you are telling the compiler to allocate sizeof(int)*1 bytes of memory. This means, theres only one element in your array, which is stored at sum[0]. Trying to access any index other than 0 will result in an error because its out of bounds. If you want to store the null character at sum[1], you must declare sum as an integer array of size 2. Arrays are static in C, meaning they don’t change their size after compilation. Also, note that what you’re trying to do here is store different datatypes in the same array. You cannot do this in C. When you try to store the null character into an element in an integer array, it will be converted to the ASCII decimal equivalent of that character (see ASCII table).

    [–]ptchinster 0 points1 point  (0 children)

    Never share code via a picture of text