Jump to content
  • Advertisement
Sign in to follow this  
drjulio2002

A simple questions about Arrays!

This topic is 4675 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

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!