Thread: arbitrary datatype arguments

  1. #1
    Registered User
    Join Date
    Mar 2009

    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


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

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    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.

    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
    Portland, OR
    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:

    /* Notice the backslashes... */
    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:

    /* Notice the lack of a trailing semicolon */
    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.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  5. #5
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    return type void looks suspicios
    To be or not to be == true

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