+ 3
Is it possible to have an array of multiple data types in C|C++?
In Python one could create a list like this: h = ['google', 55, 3.89]. And Python is written in C. Does anyone know how to achieve this? Thanks.
18 Respostas
+ 6
~ swim ~ where you getting new new logics i thought he can use structure or Union 😆😄 thanks for great explanation.
+ 5
You can have a multiple data type in in c/c++ but they have to be same type .You cannot create a data type contains different type like your example .
+ 3
Lucas although ~ swim ~ have already provided you with enough examples, but by using unions I meant using them like these , this is similar to using *varient* in C++👇
https://code.sololearn.com/cH3JI6ymC6md/?ref=app
+ 2
Samsil Arefeen Hi. I know conventional data structure won't work. Therefore, i wanted to know how languages like Python, C# do that. And I wonder if anyone here knows about it.
+ 2
Thanks ~ swim ~
+ 1
There are methods to do this in C/C++ also but then that data structure will no longer be called an array.
+ 1
Arsenic Could you give an example?
+ 1
You can define a union of all the candidate data types and create an array of those.
+ 1
Arsenic how a union can be helpful here? A union can hold only one data type in runtime. I wonder what do you mean by that. Will it be possible for you to write a simple snippet?
+ 1
~ swim ~ btw, if I want to use void* array, then how to start? C doesn't have those (tupple...) as you mentioned in your solution.
+ 1
Thanks again ~ swim ~
+ 1
I don't think it's possible.
But you can use structures and put multiple data types inside it
+ 1
Yes... If you have an array of arrays 😂😎😎
0
Python... So much things behind our back... 😋🤣
0
one approach to this is to use an array of std::any