+ 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.

19th Sep 2020, 3:55 PM
Lucas
18 Antworten
+ 6
~ swim ~ where you getting new new logics i thought he can use structure or Union 😆😄 thanks for great explanation.
19th Sep 2020, 5:50 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 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 .
19th Sep 2020, 3:59 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 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
20th Sep 2020, 12:54 AM
Arsenic
Arsenic - avatar
+ 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.
19th Sep 2020, 5:05 PM
Lucas
+ 2
Thanks ~ swim ~
19th Sep 2020, 5:12 PM
Lucas
+ 1
There are methods to do this in C/C++ also but then that data structure will no longer be called an array.
19th Sep 2020, 4:01 PM
Arsenic
Arsenic - avatar
+ 1
Arsenic Could you give an example?
19th Sep 2020, 4:06 PM
Lucas
+ 1
You can define a union of all the candidate data types and create an array of those.
19th Sep 2020, 4:14 PM
Arsenic
Arsenic - avatar
+ 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?
19th Sep 2020, 4:54 PM
Lucas
+ 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.
19th Sep 2020, 5:15 PM
Lucas
+ 1
Thanks again ~ swim ~
19th Sep 2020, 5:25 PM
Lucas
+ 1
I don't think it's possible. But you can use structures and put multiple data types inside it
19th Sep 2020, 8:26 PM
Arshia
Arshia - avatar
+ 1
Yes... If you have an array of arrays 😂😎😎
20th Sep 2020, 2:59 AM
Sanjay Kamath
Sanjay Kamath - avatar
0
Python... So much things behind our back... 😋🤣
20th Sep 2020, 7:54 PM
Bernard Bordja
0
one approach to this is to use an array of std::any
21st Sep 2020, 3:04 PM
Alexandre Kawan
Alexandre Kawan - avatar