Your intuition is mostly valid: You use the square brackets operator, []
, to access the element at an indexed position within a collection or sequence. Thus
disectedString[0]
means "the first element of disectedString";
temp[0]
means "the first element of temp";
What you've gotten mixed up are the types, as commenters and @demogorgon.net's answer have explained.
Now, with modern C++ you can "play dumb" and not declare what you know the types to be:
std::string disectedString[5];
disectedString[0] = "011001";
auto temp = disectedString[0];
auto print = temp[0];
Note the use of auto
instead of a specific type name. This will work as you would like it to. You can then use use print
, and do, for example:
std::cout << print;
and this will output 0
.
By the way, I believe you should reconsider your choice of names:
- Intuitively,
print
should refer to a function, or a method, which prints things; I'd suggest first_character
or char_to_print
or just c
if you want to be brief.
temp
is no more a temporary variable than, say, print
.
- It's better to avoid variable names which contain the type name, although we sometimes sort of have to resort to that. Specifically you using the word 'string' in variable names; probably not a good idea.
- Your
disectedString
variable is not a string, it's an array of strings, which is confusing.