+ 1

how do I create an array that can contain both int and string values???????

how do I create an array that can contain both int and string values? Pls help me

16th Dec 2018, 1:33 PM
Sofia d'Atri
Sofia d'Atri - avatar
7 Réponses
+ 5
For example : int arr = ["Hi", 78, true, 21]; cout << arr
16th Dec 2018, 2:03 PM
program
program - avatar
+ 4
Hi Sofia, To create a array which contains all value type, he can contains the strings, ints, functions, values...
16th Dec 2018, 2:02 PM
program
program - avatar
+ 4
// Anna OK
16th Dec 2018, 2:36 PM
program
program - avatar
16th Dec 2018, 2:57 PM
Calviղ
Calviղ - avatar
+ 3
// Zohir Could you provide a link to a code sample in the playground where this actually works? 🤔
16th Dec 2018, 2:32 PM
Anna
Anna - avatar
16th Dec 2018, 3:43 PM
Bebida Roja
Bebida Roja - avatar
0
Sofia, Why are you trying to do this? Maybe the community can help. C++ is a statically typed language which means that data types are chosen by the programmer while working the code and strictly enforced by the compiler at compile time. Arrays are data structures where all elements in the array are of the same data type. Depending on your needs another data structure might be a better fit. If it must be an array you might choose to make an array of strings and interpret the numbers as int later, assuming none of your strings are just numbers (e.g. [50, "50"]). Another possible solution would be to use an array of void pointers (e.g. void * myArr ). You would then some int pointers (e.g. int * someInt) and string literals in the array (e.g. const char * someString). You wouldn't have an easy way of reading or changing the data, you would have to know the type of data a void pointer points to before accessing it.
17th Dec 2018, 2:14 PM
Aaron Becker
Aaron Becker - avatar