如何使通用功能使用的void *在C? [英] How to make generic function using void * in c?

查看:154
本文介绍了如何使通用功能使用的void *在C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个增量函数来增加由 1 价值
我想使它通用的,因为我不想做出不同的功能相同的功能。

I have an incr function to increment the value by 1 I want to make it generic,because I don't want to make different functions for the same functionality.

假设我要增加 INT 浮动字符 1

void incr(void *vp)
{
        (*vp)++;
}

不过,我知道问题出在解引用空指针是未定义的行为。有时,它可能会给错误:使用无效作废前pression

But the problem I know is Dereferencing a void pointer is undefined behaviour. Sometimes It may give error :Invalid use of void expression.

我的功能可按如下:

int main()
{

int i=5;
float f=5.6f;
char c='a';

incr(&i);
incr(&f);
incr(&c);

return 0;
}

问题是如何解决此问题?有没有办法解决它在某种程度上 C

将我必须定义增量()每个数据类型?如果是的话,那有什么用无效*的

will I have to define incr() for each datatypes ? if yes, then what's the use of void *

交换同样的问题()排序()。我想交换和排序各种数据类型具有同样的功能。

Same problem with the swap() and sort() .I want to swap and sort all kinds of data types with same function.

推荐答案

可以实现第一个宏:

#define incr(x) (++(x))

当然,如果你不小心这次能有令人不快的副作用。它是关于C提供虽然应用相同的操作,以任何的多种类型的唯一方法。特别是,由于宏使用文本替换来实现,通过编译器看到它的时候,你就必须字面code ++什么; ,它可以适用 ++ 妥善为您所提供的项目类型。随着指针无效,你不知道多少(如果有的话)关于实际类型,所以你不能对这些数据多少直接操作)。

Of course, this can have unpleasant side effects if you're not careful. It's about the only method C provides for applying the same operation to any of a variety of types though. In particular, since the macro is implemented using text substitution, by the time the compiler sees it, you just have the literal code ++whatever;, and it can apply ++ properly for the type of item you've provided. With a pointer to void, you don't know much (if anything) about the actual type, so you can't do much direct manipulation on that data).

无效* 通常被用来当有问题的功能并不真正需要知道所涉及的数据的确切类型。在某些情况下(例如,的qsort ),它使用一个回调函数来避免知道数据的任何细节。

void * is normally used when the function in question doesn't really need to know the exact type of the data involved. In some cases (e.g., qsort) it uses a callback function to avoid having to know any details of the data.

由于它都排序和交换,让我们来看看在的qsort更详细一点。它的签名是:

Since it does both sort and swap, let's look at qsort in a little more detail. Its signature is:

void qsort(void *base, size_t nmemb, size_t size,
           int(*cmp)(void const *, void const *));

所以,第一个是无效* 你问 - 一个指针数据进行排序。第二讲述的qsort数组中元素的数目。第三,阵列中的每个元件的尺寸。最后是一个指向可以比较各个项目,因此的qsort 并不需要知道如何做到这一点的功能。例如,某个地方里面的qsort会有一些code是这样的:

So, the first is the void * you asked about -- a pointer to the data to be sorted. The second tells qsort the number of elements in the array. The third, the size of each element in the array. The last is a pointer to a function that can compare individual items, so qsort doesn't need to know how to do that. For example, somewhere inside qsort will be some code something like:

// if (base[j] < base[i]) ...
if (cmp((char *)base+i, (char *)base+j) == -1)

同样,交换两个项目,这将通常有临时存储照片的本地阵列。它会再从数组[我] 来其临时从阵列[J] 复制字节,然后数组[我] 终于从温度阵列[J]

Likewise, to swap two items, it'll normally have a local array for temporary storage. It'll then copy bytes from array[i] to its temp, then from array[j] to array[i] and finally from temp to array[j]:

char temp[size];

memcpy(temp, (char *)base+i, size);              // temp = base[i]
memcpy((char *)base+i, (char *)base+j, size);    // base[i] = base[j]
memcpy((char *)base+j, temp, size);              // base[j] = temp

这篇关于如何使通用功能使用的void *在C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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