Thread: Function Overload in C

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    23

    Function Overload in C

    Hi everyone,
    I was just wondering, is function overload legal in C as it is in JAVA?
    In JAVA, you can have two functions (or methods) with the same name but different argument lists and the compiler would recognize which function to call based on the call.
    For example:
    Code:
    public class blah
    {
       public static void main (String args[])
       {
          ...
          talk("My name is BOB LOBLAW", 3);
       }
       void talk (String s, int t)
       {
          ...
       }
       void talk (String s)
       {
          ...
       }
    }
    In the above code, the compiler would automatically call the first talk method because of the different argument lists.

    Is this possible in C, too?
    Thanx for your time,
    Cikotic
    If pointers have made you suicidal, you're not alone. But there is no need to hurt yourself. There are people who are willing to help. Just call your local C Crisis Centre and talk to the professtionals there. If you don't have a C3 in your neighborhood, go to the global C3 at www.cprogramming.com . If you're still suicidal, don't lose hope. Gently close your book on C and throw it in the fireplace. If you live in a tower, you can throw it out the window. There, doesn't that feel better?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    No. C++ can do it, but not C. It seems to be unique to object oriented languages.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >No. C++ can do it, but not C.
    Of course C can. Anything can be done in C. It just takes more work, it's ugly and potentially error prone, and nobody likes it enough to waste their time with it in favor of less "civilized" methods.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I'm curious Prelude. Why don't you show me some C code in which you overload a function?

  5. #5
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    i guess you need to have a look at functions with variable number of arguments

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    And the difference between that and an overloaded function would be?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why don't you show me some C code in which you overload a function?
    One way is to use function pointers:
    Code:
    #include <stdio.h>
    
    void f1(int x) { printf("f1: %d\n", x); }
    void f2(char *s) { printf("f2: %s\n", s); }
    
    int
    main(void)
    {
      void (*f)();
    
      f = f1;
      f(12345);
      f = f2;
      f("Not what you were expecting?");
    
      return 0;
    }
    Alternatively, you could fiddle with macros as well:
    Code:
    #include <stdio.h>
    
    #define f(overload) (*pf[overload])
    
    void f1(int x) { printf("f1: %d\n", x); }
    void f2(char *s) { printf("f2: %s\n", s); }
    
    static void (*pf[])() = {
      f1, f2
    };
    
    int
    main(void)
    {
      f(0)(12345);
      f(1)("Really ugly, no?");
    
      return 0;
    }
    Or you could play any number of games. But in the end, none of them are safe, and they're all pretty ugly for this purpose. And before you say that it isn't C++ style overloading, I know. The C language doesn't support that kind of feature, so you have to fake it. But that doesn't mean it isn't possible in some fashion or another.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM