创建一个接受不同数据类型参数的C函数 [英] Create a C function that accepts parameters of different data types

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

问题描述

我对C编程语言比较陌生,我试图找出如何创建一个可以接受不同类型数据作为参数的函数。该函数应该计算并返回字符或整数数组中元素的数量。我已经有两个单独的函数可以做到这一点,但我真的希望能够为这两个任务使用一个函数。有没有办法做到这一点在C?



在此先感谢!

解决方案

在C中没有标准的函数重载(也没有模板),但你可以看看printf-like函数(或 variadic functions ),也许他们可以做你需要的。如果他们允许一个灵活的参数列表。



有一个例子 here 这样的一个函数,它需要一个可变大小的整型数组。

也许你可以有一个函数签名,比如 void iterate(const char * format,...); 你可以使用以下方式:

  iterate(char,some_char_array); //对于char数组/字符串

或者

  iterate(int,some_int_array); //对于整数数组

Aniket说得很好,你如何计算整数中的元素阵列?如果你传递一个int数组作为参数,那么你也需要传递这个大小,这就违背了数组中元素计数的目的(正如你已经知道的那样,即大小)。



我假设你不知道大小,但是你在数组中有一个终止符值(例如-1)。



我根据上面的假设做了一些快速的操作。

  #include < STDARG.H> 
#include< stdio.h>
#include< string.h>

int iterate(const char * format,...)
{
va_list ap;
va_start(ap,format);

if(strcmp(format,char)== 0)
{
char * array = va_arg(ap,char *);

return strlen(array);

else if(strcmp(format,int)== 0)
{
int j = -1;
int * int_array = va_arg(ap,int *);
while(int_array [++ j]!= -1)
;
return j;
}
返回0;


int main()
{
printf(%d\\\
,iterate(char,abcdef));
int arr [] = {5,4,3,2,1,0,-1};
printf(%d \\\
,iterate(int,arr));

返回0;



$ b $ p
$ b



  $ ./a.out 
6
6


I'm relatively new to the C programming language, and I'm trying to figure out how to create a function that can accept different types of data as parameters. The function is supposed to count and return the number of elements in a character or integer array. I already have two separate functions that will do this, but I would really like to be able to use one function for both tasks. Is there a way to do this in C?

Thanks in advance!

解决方案

There is no standard function overloading in C (nor are there templates), but you could probably look into "printf-like" functions (or variadic functions) and maybe they can do what you need. If anything they allow for a flexible parameter list.

There is an example here of such a function that takes a variable size integer array.

Perhaps you could have a function signature such as void iterate(const char* format, ...); that you use in the following ways:

iterate("char", some_char_array); // for char arrays/strings

Or

iterate("int", some_int_array); // for integer arrays

Aniket makes a good point though, how do you count the elements in an integer array? If you pass an int array as an argument, you would need to pass the size too which defeats the purpose of counting the elements in the array (as you already know that i.e. the size).

I assume you don't know the size but you have a terminator value in the array (such as -1).

I've hacked something quick that kinda does what you need with the above assumption in mind.

#include <stdarg.h>
#include <stdio.h>
#include <string.h>

int iterate(const char* format, ...)
{
    va_list ap;
    va_start(ap, format);

    if (strcmp(format, "char") == 0)
    {
        char* array = va_arg(ap, char*);

        return strlen(array);
    }
    else if (strcmp(format, "int") == 0)
    {
        int j = -1;
        int* int_array = va_arg(ap, int*);
        while (int_array[++j] != -1)
                    ;     
        return j;
    }
    return 0;
}

int main()
{
    printf("%d\n", iterate("char", "abcdef"));
    int arr[] = {5, 4, 3, 2, 1, 0, -1};
    printf("%d\n", iterate("int", arr));

    return 0;
}

This prints:

$ ./a.out 
6
6

这篇关于创建一个接受不同数据类型参数的C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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