Non-const declaration of array

后端 未结 8 891
你的背包
你的背包 2020-12-19 05:11

I have been teaching myself programming for couple of years, and I was sure that if you need array declaration of a variable number you need to use malloc or

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-19 05:33

    This depends if you are writing C or C++. I'll assume C as for c++ you would be better off using std::vector rather than an array.

    In C it depends which versiuon you are using. If and only if you are using a C99 standard compiler then the array can take its size from a variable at run time as you do here otherwise the size must be defined at compile time. Visual Studio does not support the dynamic array - see MSDN

    C++ uses the C89 standard so requires the size to be set at compile time.

    So in your case you need to see what flags you passed to the compiler.

    As noted by @Eric the code is C++ so the working compiler is using a non standard extention so for gnu I would add flags to enforce a standard e.g. -ansi or -std=c++98 and -pedantic

提交回复
热议问题