I have seen an array struct in C that can hold data even if they are a different type. I forgot what that code was and I don't have the code on me at the moment. So I was wondering if anyone could show me how I would do this in C++.

Recommended Answers

All 8 Replies

I have seen an array struct in C that can hold data even if they are a different type.

This would be my best guess... but I'm open for suggestions:

void* array[4] = {5, 'Q', 2.7, 3.14159};

int* i = array;
char* c = array;
double* d = array;
float* f = array;

for(int i=0; i<4; i++)
{
     switch(i)
     {
          case 0:  cout << "Array as integer: ";
                       array = static_cast<int*>(array);
                       break;
          case 1:  cout << "Array as char: ";    
                       array = static_cast<char*>(array);
                       break;
          case 2:  cout << "Array as double: ";
                       array = static_cast<double*>(array);
                       break;
          case 3:  cout << "Array as float: ";    
                       array = static_cast<float*>(array);
                       break;
     }
     for(int j=0; j<4; j++)
 
          cout << array[j] << '\t';
 
     cout << endl;
}

void pointers
The void type of pointer is a special type of pointer. In C++, void represents the absence of type, so void pointers are pointers that point to a value that has no type (and thus also an undetermined length and undetermined dereference properties).

This allows void pointers to point to any data type, from an integer value or a float to a string of characters. But in exchange they have a great limitation: the data pointed by them cannot be directly dereferenced (which is logical, since we have no type to dereference to), and for that reason we will always have to cast the address in the void pointer to some other pointer type that points to a concrete data type before dereferencing it.

You code has alot of errors:

------ Build started: Project: classeslol, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(6) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(6) : error C2440: 'initializing' : cannot convert from 'char' to 'void *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(6) : error C2440: 'initializing' : cannot convert from 'double' to 'void *'
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(6) : error C2440: 'initializing' : cannot convert from 'double' to 'void *'
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(9) : error C2001: newline in constant
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(9) : error C2143: syntax error : missing ';' before 'constant'
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(10) : error C2144: syntax error : 'double' should be preceded by ';'
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(13) : error C2065: 'a' : undeclared identifier
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(14) : error C2440: '=' : cannot convert from 'void *[4]' to 'char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(15) : error C2440: '=' : cannot convert from 'void *[4]' to 'double *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(22) : error C2440: 'static_cast' : cannot convert from 'void *[4]' to 'int'
        There is no context in which this conversion is possible
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(25) : error C2440: 'static_cast' : cannot convert from 'void *[4]' to 'char'
        There is no context in which this conversion is possible
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(28) : error C2440: 'static_cast' : cannot convert from 'void *[4]' to 'double'
        There is no context in which this conversion is possible
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(31) : error C2440: 'static_cast' : cannot convert from 'void *[4]' to 'int'
        There is no context in which this conversion is possible
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(36) : error C2001: newline in constant
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(36) : error C2143: syntax error : missing ']' before 'constant'
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(41) : fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(5)' was matched
Build log was saved at "file://c:\Documents and Settings\tom\My Documents\Visual Studio 2008\Projects\classeslol\classeslol\Debug\BuildLog.htm"
classeslol - 17 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

After your edit it still gives me errors:

c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(6) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(6) : error C2440: 'initializing' : cannot convert from 'char' to 'void *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(6) : error C2440: 'initializing' : cannot convert from 'double' to 'void *'
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(6) : error C2440: 'initializing' : cannot convert from 'double' to 'void *'
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(8) : error C2440: 'initializing' : cannot convert from 'void *[4]' to 'int *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(9) : error C2440: 'initializing' : cannot convert from 'void *[4]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(10) : error C2440: 'initializing' : cannot convert from 'void *[4]' to 'double *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(11) : error C2440: 'initializing' : cannot convert from 'void *[4]' to 'float *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(18) : error C2440: 'static_cast' : cannot convert from 'void *[4]' to 'int *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(21) : error C2440: 'static_cast' : cannot convert from 'void *[4]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(24) : error C2440: 'static_cast' : cannot convert from 'void *[4]' to 'double *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tom\my documents\visual studio 2008\projects\classeslol\classeslol\main.cpp(27) : error C2440: 'static_cast' : cannot convert from 'void *[4]' to 'float *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Build log was saved at "file://c:\Documents and Settings\tom\My Documents\Visual Studio 2008\Projects\classeslol\classeslol\Debug\BuildLog.htm"
classeslol - 16 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Are you talking about Unions? They can still be used in C++ but they can still only hold one item at a time, of a predefined set of types.

If you want it to be an array you can do what Clinton was doing minus the mistakes. Try this code, it compiles just fine under gcc

#include <iostream>
using namespace std;

int main()
{
    int a = 1;
    double b = 2.0;
    char c = 'c';
    string d = "It works!!!\n";

    void* array[4] = {&a, &b, &c, &d};

    cout << "array[0] = " << *(int*)array[0] << endl;
    cout << "array[1] = " << *(double*)array[1] << endl;
    cout << "array[2] = " << *(char*)array[2] << endl;
    cout << "array[3] = " << *(string*)array[3] << endl;

    return 0;
}

Also, note that using void* makes it very easy to make mistakes. I wouldn't recommend doing that. Use a proper struct, class, or union and you'll make life a lot easier for yourself.

commented: Good clear example on a topic that had me stumped. +5

thank you necrolin... just out of curiosity, i've been trying to get my little piece of code to work (with limited results). your example is greatly appreciated.

How about using strings to represents float, char, double, int, and so on.

string Types[4] = { "124","1.234","aString","c" };

Then you can convert it accordingly with sstream.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.