Is it possible to have an array of multiple data types in C|C++? | SoloLearn: Learn to code for FREE!

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

c++ c

9/19/2020 3:55:07 PM

Lucas

18 Answers

New Answer

+18

#include <iostream> #include <tuple> #include <variant> using namespace std; int main() { tuple<string, int, double>T[] { {"abc", 1, 1.0}, {"def", 2, 2.0}, {"ghi", 3, 3.0} }; for(auto const& [str, ival, dval] : T) { cout << str << ' ' << ival << ' ' << dval << '\n'; } cout << '\n'; variant<int, string, char, double> mdt[] {1, "mystring", 'c', 5.5}; int i=2; cout << get<int>(mdt[0]) << '\n'; cout << get<string>(mdt[1]) << '\n'; cout << get<char>(mdt[i]) << '\n'; cout << get<double>(mdt[3]) << '\n'; } // #using std::any #include <any> int main() { any a[] {"abcd", 1, 4.5, 'E'}; cout <<any_cast<char const*>(a[0])<< '\n'; cout << any_cast<int>(a[1]) << '\n'; cout << any_cast<double>(a[2]) << '\n'; cout << any_cast<char>(a[3]) << '\n'; } Some ways 1. Array of void* 2. Array of C/C++ structs, class, union 3. Array of tuple 4. Array of variants(typesafe union) 5. Array of std::any Tough none as flexible as python list. tuple and variant are C++ only Every datatype in python is an object (PyObject or PyObject*) Python can store them as array of PyObject or dictionary of PyObjects. My guess not 100% sure.

+9

#include <stdio.h> int main() { void* mdt[4]; int i = 5; char c = 'c'; double d = 9.9; const char* str = "mystr"; mdt[0] = &i; mdt[1] = &c; mdt[2] = &d; mdt[3] = (void*)str; printf("%i\n", *(int*)mdt[0]); printf("%c\n", *(char*)mdt[1]); printf("%f\n", *(double*)mdt[2]); printf("%s\n", (const char*)mdt[3]); } Not really worth, looping is difficult. Need additional bookkeeping to keep track of exact type stored.

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

+2

🗡️Jasprit Thanks 🙂👍 Just trying to keep myself updated.

+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