-1

here's the image of that program

While I tried to print the values of array suppose of length x, its prints correctly but when I tried to print the array with different length rather than predefined length then its giving random values ... how .. ?

Aayush
  • 11
  • 2
  • 3
    Please add code as text, using the code formatting! – hyde Sep 17 '22 at 07:08
  • To see how it works, you need to run it under debugger in _disassembly_ view. Then you see what the produced machine code does. Note that the machine code will be different for different optimization levels, so what it does may change. This is because it is *Undefined Behavior*, so the code doean't need to do anything predictable. – hyde Sep 17 '22 at 07:13
  • You may want to read this: [Why not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/12149471) – Andreas Wenzel Sep 17 '22 at 07:33
  • 1
    Does this answer your question? [How dangerous is it to access an array out of bounds?](https://stackoverflow.com/questions/15646973/how-dangerous-is-it-to-access-an-array-out-of-bounds) – Gerhardh Sep 17 '22 at 09:06

1 Answers1

2

Accessing an array out of bounds* will give undefined behavior. It will compile and run, but the results are not predictable.

  • Arrays in C are indexed starting at 0. An array of nine elements has a maximum index of 8. You have accessed elements 9 and 10.
Chris
  • 26,361
  • 5
  • 21
  • 42