Sign in to follow this  
drjulio2002

A simple questions about Arrays!

Recommended Posts

Hello Everyone! Is it legal in Dev- C++, programming in C, to create an array of, an unknown number of elements, and then assign a significant number of elements to that array that was recently created?
int main()
{
 int number_of_names_entered[];
 number_of_names_entered[random_numbers_entered] = 0;
 system("PAUSE");
 return 0;
}

Share this post


Link to post
Share on other sites
No, an array size must be known when you define it. what you can do is to create a pointer and allocate to it more memory when needed, but it would be much easier in C++ using classes.
or just allocate a maximum memory (up to your needs).

Share this post


Link to post
Share on other sites
Unless this is some wierd compiler-specific behavior of Dev-C++ no.

Your code will probably compile fine but when you run it you will corrupt memory causing anything from nothing to crashes to wierd behavior.

If you want a variable sized array in C you have to use pointers and malloc/free. In C++ use std::vector for the vast majority of cases.

Share this post


Link to post
Share on other sites

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