Пожалуйста, подпишитесь на официальный канал Codeforces в Telegram по ссылке https://t.me/codeforces_official. ×

Блог пользователя ublog

Автор ublog, 13 лет назад, По-английски

Why this code works? why it didn't get Runtime & compilation error? (runtime for a[-1] and compilation error for a[n]) maybe new c++ futures ??!!

int n;
int main () 
{
    cin>>n;
    int a[n];
    a[-1]=40;
    cout<<a[-1];
    return 0;
}
Теги c++
  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
13 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится -8 Проголосовать: не нравится

С++ reserves that cell in memory

»
13 лет назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

This question is very popular among beginner users of C++. Here is some answer. And here is a bunch of another.

»
13 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

And yes, int a[n] is valid code, it works like int *a = new int[n]. As I remember for C, it works in C99, not elder versions.