How can I initialize an index giving its size thru an int variable aiming the something like the above:
int x=10;
char test[x];
Thank you,
Thiago Santana
This is a discussion on Array initialization with int variable within the C++ Programming forums, part of the General Programming Boards category; How can I initialize an index giving its size thru an int variable aiming the something like the above: int ...
How can I initialize an index giving its size thru an int variable aiming the something like the above:
int x=10;
char test[x];
Thank you,
Thiago Santana
std::vector<T> v(size);
But if it's just a string, use std::string.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
Thanks Elysia,
But quite not what I am looking for. I just want to inform the simple array size being the size an int variable. It has to be a very simple array like the one I have sample on my original post.
Thank you,
Thiago Santana
Are you looking into making a dynamically sized array?
For example, setting a size and making an array that big, or are you looking for some string?
I don't get your question really.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
You can't do that legally with a variable. If you know x will be 10 (or some other number) when you write the program use this:Some compilers allow your original code as an extension, but as I said that's not strictly legal in C++.Code:const int x=10; char test[x];
If the size is not a constant (and you want to use standard C++), then you have no choice but to use a dynamic array container. The best choice for that is vector (or string) as shown by Elysia. You can also use new[]/delete[], but that would make sense only if your instructor doesn't allow you to use vector for some reason.
Thanks Daved and Elysa for the inputs. That helps me.