Array bound is not an integer constant before being resolved by declaring an array inside the main

program 1

#include <iostream>

std::size_t three() {
    return 3;
}

int i[three()];

int main() 
{
    return 0;
}

program 2

std::size_t three() {
    return 3;
}

int main() 
{
    int i[three()];
    return 0;
}

The problem here in program 1, as expected, gives a compilation error

": array bound is not an integer constant before the ']' token

But I have no idea why program 2 compiled successfully?

+4
source share
1 answer

C99 int i[three()]; , . , , C99. main() , , , , C99.

, GCC Clang, C89 ++ . ++ . GCC Clang , -pedantic.

+4

Source: https://habr.com/ru/post/1548721/


All Articles