Sign in to follow this  

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

This topic is 2548 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
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
Ah I should specify, I used 2 of those methods using std::string gave me the same error. I need to read up on dynamic arrays and just wanted to be sure it wasn't just this compiler that the problem was in seeing as how in 2008 it compiles just fine.

So to answer your question, yes I read the replies but wanted to rule out any other fixes because this is the main compiler I have to use for school and need to be aware of any such issues and fixes

Share this post


Link to post
Share on other sites
I'm using char arrays just because that's what I have the most experience in. I just finished my intro c++ course(with an A ^_^) and want to keep practicing before intermediate programming, so I was switching to studio 2010 and I'm learning much more about the language from it but some of the things I've been taught don't seem to be legal here

Share this post


Link to post
Share on other sites

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