Cannot assign value to global array in c++

前端 未结 2 1737
栀梦
栀梦 2021-01-15 20:27

I\'ve got this code:

 #include 
    int tabela[1];
    tabela[0] = 1;
    int main(){
        std::cout << tabela[0];
        std::cin.         


        
相关标签:
2条回答
  • 2021-01-15 21:08

    At the outermost level, a C++ file is a sequence of declarations. tabela[0] = 1; is not a declaration - it's a statement (in particular an expression-statement). A function body, however, is a sequence of statements, so it's fine to put this line inside the body of main (or any other function).

    Some statements are declarations (called declaration-statements), but in general they're not.

    0 讨论(0)
  • 2021-01-15 21:08

    for it to be valid C++, you can only initialize variables in global, you can't assign them there.

    edit: comments beat me to it. props

    0 讨论(0)
提交回复
热议问题