如何编写C函数接受任何类型的(一个)参数 [英] How to write C function accepting (one) argument of any type

查看:188
本文介绍了如何编写C函数接受任何类型的(一个)参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我采取简单的库在C列表,我有写问题找到功能。

I am implementing simple library for lists in C, and I have a problem with writing find function.

我想我的函数接受的任何的参数类型找到,既:
找到(my_list,3)找到(my_list,my_int_var_to_find)
我已经拥有的信息是什么列表的元素类型

I would like my function to accept any type of argument to find, both: find(my_list, 3) and find(my_list, my_int_var_to_find). I already have information what is type of list's elements.

现在我已经找到了几个处理这个方式:

For now I've found couple of ways dealing with this:


  • 不同的功能与后缀不同的类型: INT遇见(void *的列表,int i)以 INT findd(void *的列表,双D) - 但是我不喜欢这种做法,似乎冗余我和API是混乱

  • different function with suffix for different types: int findi(void* list, int i), int findd(void* list, double d) - but I don't like this approach, it seems like redundancy for me and an API is confusing.

使用union:

typedef union {
   int i;
   double d;
   char c;
   ...
} any_type;

但这种方式,我强迫用户都知道关于 any_type 工会,以及调用之前创建它找到 。我想避免这种情况。

but this way I force user to both know about any_type union, and to create it before invocation of find. I would like to avoid that.

使用可变参数函数: INT找到(void *的列表,...)。我喜欢这种方法。不过,我担心对参数的数目没有限制。用户可以自由地写 INT X =发现(表1,2.0,'C')虽然我不知道它应该意味着

using variadic function: int find(void* list, ...). I like this approach. However, I am concerned about no restrictions on number of arguments. User is free to write int x = find(list, 1, 2.0, 'c') although I don't know what it should mean.

我看到也回答了这个问题:<一href=\"http://stackoverflow.com/questions/10212655/c-send-different-structures-for-one-function-argument\">C :发送不同的结构,一个函数的参数但它是无关紧要的,因为我要接受非指针参数。

I have seen also answer to this question: C : send different structures for one function argument but it's irrelevant, because I want to accept non-pointer arguments.

什么是处理这个功能的正确方法?

What is the proper way of handling this function?

推荐答案

您可以改为尝试实现你的功能类似于像 bsearch 泛型函数,它可以执行二进制搜索任何数据类型的数组上:

You could instead try implementing your function similar to a generic function like bsearch, which can perform a binary search on an array of any data type:

void *bsearch(const void *key, const void *base, size_t nmemb, size_t size,
              int (*compar)(const void *, const void *))

而不是硬编码的函数内部不同的数据类型不同的实现,而不是你的指针传递给一个函数,它会做的类型相关的操作,只有它知道底层的实现。你的情况,这可能是某种遍历/迭代函数的。

Rather than hard-coding the different implementations for different data types inside your function, you instead pass a pointer to a function which will do the type-dependent operation, and only it knows the underlying implementation. In your case, that could be some sort of traversal/iteration function.

的另一件事 bsearch 需要知道(除了明显 - 搜索键和阵列长度)为阵列中的每个元件的尺寸,这样它可以计算阵列中的每个元素的地址,并把它传递给比较功能

The other thing bsearch needs to know (apart from the obvious - search key and array length) is the size of each element in the array, so that it can calculate the address of each element in the array and pass it to the comparison function.



如果你有那是要操作的类型有限的名单,没有什么不妥有一个家庭的findX()功能。上述方法要求每个数据类型将被传递到 bsearch 功能,然而的主要区别之一是共同的功能并不需要重复和一个功能泛型函数可用于的任何的数据类型。


If you had a finite list of types that were to be operated on, there's nothing wrong with having a family of findX() functions. The above method requires a function for each data type to be passed to the bsearch function, however one of the main differences is that common functionality doesn't need to be repeated and the generic function can be used for any data type.

我真的不能说有任何的正确的方式做到这一点,就看你真的取决于你试图解决的问题。

I wouldn't really say there's any proper way to do this, it's up to you and really depends on the problem you're trying to solve.

这篇关于如何编写C函数接受任何类型的(一个)参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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