C語言實現不同類型傳入一個型別參數,分別以相應的不同類型以同一個參數輸出

2020-08-14 11:06:38
enum type
{
	CHAR,
	UCHAR,
	INT,
	UINT
};

int man()
{
	unsigned int data = 0;//data的型別可以隨意指定
	InFunction(INT,&data);//輸入型別,轉化爲自己想要的型別
	OutFunction(UINT,(void *)&data);//根據自己想要的型別輸出
	printf("%u\n",data);
}

void OutFunction(enum type,void* data)
{
	int start = 0;
	switch(type)
	{
	    case UINT:
	        unsigned int temp = (unsigned int)start;
	        *(unsigned int*)data = temp;
	        break;
	    default:
	        break;
	}
}


void InFunction(enum type,void* data)
{	
	unsigned int signal = 0;
	swich(type)
	    case INT:
	        int* temp = (int*)data;
	        signal = (unsigned int)(*temp);
	    break;
	    default:
	    break;
}