multiple input type

This is a discussion on multiple input type within the C Programming forums, part of the General Programming Boards category; Can you have a function that has an argument that could be either one type or another. For example foo(x ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    36

    multiple input type

    Can you have a function that has an argument that could be either one type or another. For example foo(x input) where x is either char or long int? should i look into enums?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Depending on what you're doing with the input variable, you might not need to change the type. For instance, you can still pass a character to a function accepting a long int.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    What you're talking about modec, is called function overloading which isn't supported in C.

    I'm sure there would be some way around it. (there usually is in C)

    How about passing the variables in a union parameter? you would also need to also pass something that tells your function what type the argument is.

    I would post some code here, but my compiler's down at the moment. (using my bro's machine) - I'm not sure if this is allowed :

    Code:
    void foo(int type, union x{char c; long int l})
    {
    ...
    }

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Hmm, let me show you what I meant:
    Code:
    itsme@itsme:~/C$ cat fake.c
    #include <stdio.h>
    
    void printit(long int num)
    {
      putchar((int)num);
    }
    
    int main(void)
    {
      printit('A');
      printit('\n');
    
      return 0;
    }
    itsme@itsme:~/C$ gcc -Wall fake.c -o fake
    itsme@itsme:~/C$ ./fake
    A
    itsme@itsme:~/C$
    Now I'm not sure you're trying to print the character or what, but if you want to do two different things depending on the datatype of the parameter then you should really just have 2 different functions. However, if you're just going to be passing a "smaller" integer into the function then make the function parameter the "biggest" data type and go with it like I did in my code.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    This probably won't help, but as to function overloading in C, Prelude showed us some techniques here:
    Function Overload in C
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    thanks everyone. itsme86 the function is exactly the same for the two types and using long for both long and char inputs works just fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Replies: 5
    Last Post: 02-25-2008, 05:35 AM
  3. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  4. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  5. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21