Jump to content
  • Advertisement
Sign in to follow this  
link161

Since when can't you use a variable in a char array in c++?

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

I'm using VS2010 and it should be a simple declaration:
len=strlen(mine);
char guessed[len];

it keeps telling me it needs to be a constant value, but I've declared this before.

I even tried char guessed[strlen(len)]
and get the same error, any help?

Share this post


Link to post
Share on other sites
Advertisement
The size of an array needs to be statically known. There are couple of options for you :

1)Use std::string
2)Use std::vector<char>
3)Use dynamic arrays
4)Create a large char array, ex char array[1000]; and have a variable for its length
5)Implement your own.

Share this post


Link to post
Share on other sites
The compiler is right about showing an error, this is not legal.

However:
1. it is legal if you do it with a constant string, since the compiler will be able to determine that the length is actually constant (so char* x[strlen("foo")]; is legal)
2. it is legal with gcc, since gcc supports non-constant-length arrays as a GNU extension

Share this post


Link to post
Share on other sites
Oh, I didn't expect so many responses thank you guys. I'll start trying some different solutions here, it's just odd because I remember being able to do this before and not having any complications. I just switched to VS2010 from 2008 and I don't know if I like it too much lol

Share this post


Link to post
Share on other sites
Ok it seems to just be a Visual Studio 2010 error, I've run my code in dev c++ and studio 2008 and I don't get so much as even a warning. I learned it that way but the new studio just won't let it go through, why is this? and is there any way to get it to work on here?

Share this post


Link to post
Share on other sites
Quote:
but the new studio just won't let it go through, why is this?
Did you read the responses? "The size of an array needs to be statically known" and "The compiler is right about showing an error, this is not legal."

Quote:
is there any way to get it to work on here?
Either use a compiler that supports VLAs as an extension, in the process writing compiler specific code, or do what Concentrate said (the very first reply). You got 4 options there...

Share this post


Link to post
Share on other sites
It's correct for this to be an error, you should not be able to do so, and I'm surprised it worked for you in Visual Studio 2008.

Why are you trying to use a char array? You're almost certainly better off using an std::string or an std::vector of chars.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!