-1

Declaration for an array in C is:

type name [elements];

So why doesn't this code throw an exception such as "out of bounds" or any other exception?

#include <stdio.h>
void main()
{
    int a[5];
    a[7]=75;
    printf("%d",a[7]); 
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Lavi
  • 27
  • 1
  • 6
  • because there is no such exception. You can do wrong things and bad things will happen. Its you who has to make sure that your code is correct – 463035818_is_not_an_ai Jul 30 '18 at 15:08
  • read about undefined behaviour. There is a huge list of stuff you can do wrong and the compilers are allowed to emit no error or warning but just silently do anything – 463035818_is_not_an_ai Jul 30 '18 at 15:09
  • it is going to store that value in memory directly after your defined array. If something else is using whatever is defined after it could get ugly. – M Y Jul 30 '18 at 15:09
  • This a good explanation about array length and bounds checking in C: https://softwareengineering.stackexchange.com/a/237290 – Alex Johnson Jul 30 '18 at 15:12
  • 1
    @FBergo I wouldnt claim that `std::vector` was introduced to make c++ more "idiots friendly" ;) – 463035818_is_not_an_ai Jul 30 '18 at 15:22
  • the code is almost a duplicate of [Out of index C/C++](https://stackoverflow.com/q/26364095/995714) – phuclv Jul 30 '18 at 15:24
  • C does not require any bounds checking on array accesses, so as long as you don't do anything that causes the memory manager or operating system to complain (like cross a page boundary or something), you won't get any kind of run-time error. If you write to something outside of the array bounds, you may get a run-time error later, depending on what you overwrote. – John Bode Jul 30 '18 at 17:50

1 Answers1

0

Because in C the variable with the name of the array is the pointer to the first memory-cell of the array and the number between brackets is the offset of the data you want from the starting of the array, it doesn't check if it is a memory cell of the array or not.

Marco Rocchi
  • 138
  • 1
  • 10
  • Arrays are not pointers and this confusion needs to cease. – Quentin Jul 30 '18 at 15:22
  • I didn't say it... – Marco Rocchi Jul 30 '18 at 15:23
  • Actually, it *is* the pointer to the first (technically 0th index) element of the array. That is why calling `*p` works, pointer arithmetic etc – EpicPandaForce Jul 30 '18 at 15:26
  • 2
    @EpicPandaForce: An array is not a pointer and a pointer is no array. Just like Quentin said: thi confusion needs to cease! [conv.array]/1: An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” *can be converted* to a prvalue of type “pointer to T”. The temporary materialization conversion ([conv.rval]) is applied. The result is a pointer to the first element of the array. – Swordfish Jul 30 '18 at 15:36
  • 3
    Oh, the question is tagged "C" ... well. [6.3.2.1 Lvalues, arrays, and function designators]/3: Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' *is converted to an expression with type ''pointer to type''* that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined. – Swordfish Jul 30 '18 at 15:52