Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to typecast personal structure.
but i dont know how do i typecast.
is anyone to solve this problem?


C++
typedef struct _data_type
{

	INT	   n;
	BOOL   b;
	double db;
	DWORD  dw;
	DOUBLE DB;
	
}DATATYPE;

#define CAST(data,str) (##str##*##)##data

void * data;
str ="DATATYPE"

CAST(data,str);


What I have tried:

is it impossible? plz, answer my question.
Posted
Updated 14-May-16 9:41am
v3
Comments
Homero Rivera 14-May-16 14:10pm    
Isn't void supposed to mean, "no result to return" for methods? Maybe you need null?
nullptr ?
Sergey Alexandrovich Kryukov 14-May-16 14:43pm    
To start with, why?
—SA
PJ Arends 14-May-16 15:18pm    
Do not use void* if you can at all avoid it. You loose all type safety if you use it.

The cast you are trying to do is better done with reinterpret_cast.

1 solution

I assume you are using C programming language (since typedef before struct isn't necessary in C++).

In C programming language, the cast could be written
C
void * data;
DATATYPE * d0 = (DATATYPE *) data;


Hence your macro would become
C
#define CAST(data, DATATYPE)  (DATATYPE*)(data)


Usage:
C
DATATYPE * d1 = CAST(data, DATATYPE);


However, I question the usefulness of such a macro.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900