using void** pointer

Hello Everybody,

I want to use void** pointer as a function arguement.
But I am not able to use it as it is giving many errors.
using single void pointer is working fine, but when i want to handle the whole 2-d array with the void pointer i am getting some errors.

here is my code..


#include <iostream>
using namespace std;
enum Type
{
INT,
FLOAT,
STRING,
UNSIGNED_INT,
};

void Print_1(void** pVal, Type eType)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<static_cast<unsigned int>pVal[i][j]<<" ";
}
cout<<endl;
}
}

void Print(void *pValue, Type eType)
{
switch (eType)
{
case INT:
cout << *static_cast<int*>(pValue) << endl;
break;
case FLOAT:
cout << *static_cast<float*>(pValue) << endl;
break;
case STRING:
cout << static_cast<char*>(pValue) << endl;
break;
}
}

int main()
{
int nValue = 5;
float fValue = 7.5;
char *szValue = "Mollie";
unsigned int** nVal;
nVal = new unsigned int*[3];
for(int i=0;i<3;i++)
nVal[i] = new unsigned int[3];

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
nVal[i][j] = 10*(j+1);
}
Print(&nValue, INT);
Print(&fValue, FLOAT);
Print(szValue, STRING);
Print_1(tt, UNSIGNED_INT);
return 0;
}

and these are the errors.

testing.cpp: In function ‘void Print_1(void**, Type)’:
testing.cpp:17: error: expected ‘(’ before ‘pVal’
testing.cpp:17: warning: pointer of type ‘void *’ used in arithmetic
testing.cpp:17: error: ‘void*’ is not a pointer-to-object type
testing.cpp:17: error: expected ‘)’ before ‘;’ token
testing.cpp: In function ‘int main()’:
testing.cpp:43: warning: deprecated conversion from string constant to ‘char*’
testing.cpp:59: error: invalid conversion from ‘unsigned int**’ to ‘void**’
testing.cpp:59: error: initializing argument 1 of ‘void Print_1(void**, Type)’

please help me solving these errors.

Thank You

Nikunj
Well, this cout<<static_cast<unsigned int>(pVal[i][j])<<" "; // Note ()

what is tt?
sorry, i will update the code. i have written, some wrong variables..

#include <iostream>
using namespace std;
enum Type
{
INT,
FLOAT,
STRING,
UNSIGNED_INT,
};

void Print_1(void** pVal, Type eType)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<static_cast<unsigned int>pVal[i][j]<<" ";
}
cout<<endl;
}
}

void Print(void *pValue, Type eType)
{
switch (eType)
{
case INT:
cout << *static_cast<int*>(pValue) << endl;
break;
case FLOAT:
cout << *static_cast<float*>(pValue) << endl;
break;
case STRING:
cout << static_cast<char*>(pValue) << endl;
break;
}
}

int main()
{
int nValue = 5;
float fValue = 7.5;
char *szValue = "Mollie";
unsigned int** nVal;
nVal = new unsigned int*[3];
for(int i=0;i<3;i++)
nVal[i] = new unsigned int[3];

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
nVal[i][j] = 10*(j+1);
}
Print(&nValue, INT);
Print(&fValue, FLOAT);
Print(szValue, STRING);
Print_1(nVal, UNSIGNED_INT);
return 0;
}

and these are the errors.

testing.cpp: In function ‘void Print_1(void**, Type)’:
testing.cpp:17: error: expected ‘(’ before ‘pVal’
testing.cpp:17: warning: pointer of type ‘void *’ used in arithmetic
testing.cpp:17: error: ‘void*’ is not a pointer-to-object type
testing.cpp:17: error: expected ‘)’ before ‘;’ token
testing.cpp: In function ‘int main()’:
testing.cpp:43: warning: deprecated conversion from string constant to ‘char*’
testing.cpp:59: error: invalid conversion from ‘unsigned int**’ to ‘void**’
testing.cpp:59: error: initializing argument 1 of ‘void Print_1(void**, Type)’
void pointers.... ew.

1
2
3
4
void Print_1(void** pVal, Type eType)
{
    //...
    cout<<static_cast<unsigned int>pVal[i][j]<<" ";


This doesn't work because you are trying to dereference a void pointer (illegal), and cast the result.

You need to cast the pointer before you dereference it:

 
cout << (static_cast<unsigned int**>(pVal))[i][j] << " ";

Last edited on
It is still not working,

When i changed that function 'Print_1' to

1
2
3
4
5
6
7
8
9
10
11
void Print_1(void** pVal, Type eType)
{
  for(int i=0;i<3;i++)
  {
     for(int j=0;j<3;j++)
     {
          cout<< (static_cast<unsigned int**>(pVal))[i][j]<<"  ";
     }
     cout<<endl;
   }
} 


i got the following erros.


testing.cpp: In function ‘void Print_1(void**, Type)’:
testing.cpp:17: error: invalid static_cast from type ‘void**’ to type ‘unsigned int**’
testing.cpp: In function ‘int main()’:
testing.cpp:43: warning: deprecated conversion from string constant to ‘char*’
testing.cpp:59: error: invalid conversion from ‘unsigned int**’ to ‘void**’
testing.cpp:59: error:   initializing argument 1 of ‘void Print_1(void**, Type)’


Please help me out,

Thank You
ah, sorry, maybe you need reinterpret_cast instead of static_cast
Topic archived. No new replies allowed.