Thread: arbitrary datatype arguments

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    arbitrary datatype arguments

    hello evryone...
    I want to know if it is possible to write a function with the same name which can accept arguments of different datatypes..
    ex my_function(datatype x) where datatype can be int or double or char etc...
    I understand that this is very semilar to function overloading in C++... but i would like to know how this can be done in C.

    Thank u

    --Eshwar

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Perhaps a generic pointer void* which can be coerced to any type.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so in C++ it is perfectly fine to have many functions with the same name, as long as they have a different set of parameters. In C, the rules are:
    1. C only allows ONE function with a particular name, so you can not have more than one set of arguments to a function.
    2. How is the function going to know what the arguments are?

    You can sort use void * to pass a pointer to an arbitrary type. You still need some way to indicate WHAT type it is.

    Variable argument functions can also take "any type" of argument (including non-pointer types), but again, you need some way to indicate what each type is, and how it should be used.

    So, yes, there may be a way of doing this, but it's not entirely trivial.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,397
    Depending on the situation, the best way may be a macro. For example, let's use min() which returns the lower of its two arguments:

    Code:
    /* Notice the backslashes... */
    
    #define DECLARE_MIN_FUNCTION_INSTANCE(type) \
    void min_##type(type arg1, type arg2) \
    { \
        return (arg1 <= arg2) ? arg1 : arg2; \
    }
    Then, you could declare a min function for integers, and one for doubles:

    Code:
    /* Notice the lack of a trailing semicolon */
    
    DECLARE_MIN_FUNCTION_INSTANCE(int)
    DECLARE_MIN_FUNCTION_INSTANCE(double)
    Unfortunately, you still need to invoke the function by its proper name. So if you are comparing ints, you would use min_int(), and for doubles you would use min_double(). I would not recommend this approach generally, but in certain cases it can be useful.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    return type void looks suspicios
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM