Can I make an array with both numbers and characters | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Can I make an array with both numbers and characters

Is it possible in c++

8th Apr 2018, 9:40 AM
JonneSir
JonneSir - avatar
3 Answers
+ 2
array can store only one type of things but you can convert your chars to ints with (int)ch and put it in the array, because characters are internally stored as numbers
8th Apr 2018, 9:50 AM
michal
+ 5
struct numcharvec { std::vector<int> nums; std::vector<char> chars; }
8th Apr 2018, 9:51 AM
Timon Paßlick
+ 2
You may try an array of a union : union Spl{short i; char c;}; int main() { array<Spl,5> arr = {1,'2',32,'a','e'}; cout<<arr[3].c; // Prints a. }
8th Apr 2018, 10:02 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar