1

I was just going through some simple C++ concepts. I like to think that I am aware of the difference between dynamic and static arrays. But when I run the following code:

`

#include <iostream>
using namespace std;
int main()
{
    int size;
    cout<<"enter size: ";
    cin>>size;
    int arr[size];
    cout<<"enter array values: ";
    for(int i=0;i<size;i++)
        cin>>arr[i];
    for(int i=0;i<size;i++)
        cout<<arr[i]<<" ";
    return 0;
    }

`

It does not give me an error. It shouldn't let me create a static array with the size input from the user right?

8

1 Answer 1

5

You cannot. This is not valid C++.

It compiles because the compiler you use offers extensions beyond by the C++ standard. Your compiler would have warned you about it if you had enabled compiler warnings:

-Wall -Wextra -pedantic
Sign up to request clarification or add additional context in comments.

5 Comments

Note that enabling just any warnings (such as -Wall or -Wextra) is not sufficient. It is necessary to use -pedantic to enable standard conformance.
@eerorika Thanks. Amended.
... and sometimes that's not even enough. -pedantic-errors seems to help with the last bit.
@TedLyngmo -Werror and sorts are always alluring, but if one gets in the habit of visually reading warnings as errors it becomes unnecessary. Further, there are sometimes situations during debugging where you want stuff like unused arguments and friends to be displayed as warnings but to not break the build.
I got over that and usually just fix it instead of forcing the compilation through. If I don't agree with the analysis, I either remove that particular analysis step completely or for the single case by adding a comment why the code is better as-is than it would be as suggested by the linter or whatever it is that is complaining.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.