void * 类型化函数参数 [英] void * typed function parameter

查看:41
本文介绍了void * 类型化函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,void *Client(void *threaData){}

你能告诉我一些关于 void *threadData 参数的事情吗?当您使用 void * 参数时,为什么?

Can you tell me some things about void *threadData parameter. When you use void * parameter and why?

推荐答案

void * 是一个可以指向任何对象类型的通用指针.上面的函数可以接受一个指向任何类型的指针,并且可以返回一个指向任何类型的指针.

void * is a generic pointer which can point to any object type. The above function can take a pointer to any type and can return a pointer to any type.

如果不确定用户输入的数据的数据类型,可以使用通用指针.

A generic pointer can be used if it is not sure about the data type of data inputted by the user.

示例:以下函数将打印任何数据类型,提供有关数据类型的用户输入

Example: The following function will print any data type provided the user input about the type of data

void funct(void *a, int z)
{
    if(z==1)
        printf("%d",*(int*)a); // If user inputs 1, then he means the data is an integer and type  casting is done accordingly.
    else if(z==2)
        printf("%c",*(char*)a); // Typecasting for character pointer.
    else if(z==3)
        printf("%f",*(float*)a); // Typecasting for float pointer
}

这篇关于void * 类型化函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆