Sign in to follow this  

What am I missing here? array declaration confusion

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

We started coding in my c++ class today, and the instructor has
	int var;
	cin>>var;
	int array[var];


which compiles on the compilers at school but not on MVSC++ which i use. Now I thought when you declared arrays like this, the array was created at run time and I thought the only way to do something to this effect was with int *array=new int[var]; but that doesnt explain why it compiles on the comps at school or why the instructor had no clue what I was talking about. Is this compiler specific? We're using some linux compiler at school.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
well whats the error you are getting for why it won't compile for you?

Share this post


Link to post
Share on other sites
The error there is that arrays allocated to the stack need a size that can be determined at compile time, and hence the integer needs to be constant. If you want to create an array that is sized dynamically at runtime you need to use the new operator.


int arraysize = 0;
std::cin >> arraysize;
int* parray = new int[arraysize];
delete [] parray;



This is stuff that you cannot learn well over the internet forums, you need a good beginners book. Thinking in C++ is an ok book, and its free!

Share this post


Link to post
Share on other sites
It's not legal C++. Static array sizes must be compile-time constants. C99 allows variable-length arrays, but C++, at present, does not. You must indeed allocate memory dynamically—or use a vector.

Share this post


Link to post
Share on other sites
No no no , lol ,thank you guys for the replies but your making inferences! I'm not confused as to why it DOESNT work for ME, im confused as to why it DOES work for HIM and why this dude who took c++ in college has no idea what I'm talking about. I've been coding c++ for slightly over a year now and I didnt think it what he was doing was valid, but it does compile at school! I cant figure out why.

edit: yea graveyard im positive, this makes no sence.

edit2: what exaclty is C99? i guess that must be what we're doing...

Share this post


Link to post
Share on other sites
C99 is the 1999 revision of the C language standard and has nothing directly to do with C++. As for why this works on the compilers where you have seen it in action—not all compilers are entirely standard compliant; presumably whatever compiler you use has an extension to allow this functionality. If it is g++ (or MinGW, or ...), you may want to play with the flags -ansi and -pedantic to enforce standard C++.

I personally compile pretty much every C++ program I write with -Wall -Werror -ansi -pedantic ...

Share this post


Link to post
Share on other sites
Oh, whats g++ because thats the command we use to compile it. (He has us do virtually ALL file manipulation through the stupid linux console), I wonder why he didnt explain any of this from the start, let alone once i brought up my confusion.

Share this post


Link to post
Share on other sites
g++ is the C++ compiler of the GNU Compiler Collection, GCC. What, exactly, is it that he should have mentioned? I'm not sure I understand your grievance—though I obviously think that using the -Werror, -Wall, -ansi, and -pedantic options is a good idea, unless you have specific reason not to.

As for "the stupid linux console", don't knock it. I'm a Gentoo Linux user myself, and mostly a CLI man (through graphical terminals like gnome-terminal)—I've yet to find a GUI that offers me the same power, flexibility, and yes, ease of use that the bash shell offers me (save for specific purposes, such as browsing image directories—then I use Nautilus). Once you learn about redirection and piping, you'll wonder how you could ever stand using a GUI ...

As for "why this dude who took c++ in college" doesn't know what's wrong, well, a CS degree is not about the intricacies of any one language, and many a good programmer may be unaware of some of the finer nuances. Additionally, there are a lot of people who graduate from college who couldn't program their way out of a paper bag if they had a chainsaw with a C++ interface ...

Share this post


Link to post
Share on other sites
It's not as big of a deal as I've made it out to be, but it was akward questioning his syntax and getting a reply like :"That's because you want to have the array be diferent sizes" . (I almost took it as an insult.. lol, it's a second year class so why not assume the students understand at least the first month stuff of the first year class?), I should have made myself more clear to him.

Share this post


Link to post
Share on other sites

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this