Create a C function that accepts parameters of different data types

前端 未结 6 1273
陌清茗
陌清茗 2021-02-09 02:07

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

相关标签:
6条回答
  • 2021-02-09 02:47

    So, let's assume your two functions are called sizeof_char_array and sizeof_int_array.

    In C11, there is a new feature called "generic selection" that will let you do what you want with a relatively simple macro:

    #define sizeof_array(X) \
        _Generic (*(X), \
                  char: sizeof_char_array, \
                  default: sizeof_int_array) (X)
    

    (I don't even have a C11 implementation to test this against, so caveat emptor!)

    Prior to C11, this was sometimes accomplished with a macro using regularly named functions. You can define a macro that will call one function or the other depending on a macro argument hint:

    #define sizeof_array(xtype, x) sizeof_ ## xtype ##_array(x)
    
    int a[] = { 1, 2, 3, 4, -1 };
    char b[] = "abc";
    
    sizeof_array(int, a);   /* macro expands to sizeof_int_array(a) */
    sizeof_array(char, b);  /* macro expands to sizeof_char_array(b) */
    

    If the input argument is truly an array, you can use a macro to compute its size directly:

    #define ARRAY_SZ(x) (sizeof(x)/((void *)x == &x ? sizeof(x[0]) : 0))
    

    In the case of an array, the following expression is true:

    (void *)arr == &arr
    

    Because the address of an array has the same location in memory as the address of its first element.

    So, the macro computes: sizeof(arr)/sizeof(arr[0]). Since the sizeof operator reports the size in bytes of its argument, the computed expression results in the number of elements in the array. However, if you are using a sentinel to compute the length, the ARRAY_SZ macro will result in a size at least one larger than the length found traversing the array for the sentinel.

    In the case that the argument is not an array, then the expression results in a divide by 0 exception.

    0 讨论(0)
  • 2021-02-09 02:48

    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*);
    
            va_end(ap);
            return strlen(array);
        }
        else if (strcmp(format, "int") == 0)
        {
            int j = -1;
            int* int_array = va_arg(ap, int*);
            while (int_array[++j] != -1)
                        ;   
            va_end(ap);  
            return j;
        }
        va_end(ap);
        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
    
    0 讨论(0)
  • 2021-02-09 03:01

    You should make your function arguments take in a void * type. This way, you can pass in different types of data, and type-cast it to the one you want. However, you do need to watch out because there is no guaranteed way to correctly 'guess' the type that a void* points to.

    0 讨论(0)
  • 2021-02-09 03:07

    The answer is quite simple. You do need a function for this task. Just try this piece of code

    #define len(array) sizeof(array)/sizeof(*array)
    

    and that's it.

    Important note: As pointed out in the comments, this will not work for dynamically allocated arrays.

    0 讨论(0)
  • 2021-02-09 03:08

    In either case, you will need some sort of type-inferencing system to tell the C compiler which function to call. Which means, you will need to know, before-hand the type of array you might send in as a parameter to this "super function" of yours.

    There is no "auto-type-inferencing" in C that can let you reflect upon the type of data at runtime. Better yet, you might have to write your own runtime environment for this to happen.

    A slightly trivial hackish way to do this:

    #include <stdio.h>
    
    size_t GetLengthOfArray(size_t sizeOfOneElementInArray, size_t sizeOfTheArrayInBytes)
    {
       return sizeOfTheArrayInBytes/sizeOfOneElementInArray; 
    }
    int main(int argc, char *argv[])
    {
       char cArr[10] = {'A','B','C','D','E','F','G','H','I','J'};
       int iArr[5] = {10,20,30,40,50};
       printf("%d is the length of cArr\n%d is the length of iArr",GetLengthOfArray(sizeof(cArr[0]),sizeof(cArr)),
                                                                   GetLengthOfArray(sizeof(iArr[0]),sizeof(iArr)));
       return 0;
    }
    
    0 讨论(0)
  • 2021-02-09 03:09

    It's not really possible, but you can make a tagged union

    typedef struct {
        union {
            ssize_t i;
            double d;
            char *s;
        } unknown;
        char select;
    } Dynamic;
    
    

    Or you can use a void pointer:

    typedef struct {
      void * unknown;
      char identity;
    } Dynamic;
    
    0 讨论(0)
提交回复
热议问题