First of all, what is function overloading? Function overloading is a feature of a programming language that allows one to have many functions with same name but with different signatures.
This feature is present in most of the Object Oriented Languages such as C++ and Java. But C (not Object Oriented Language) doesn’t support this feature. However, one can achieve the similar functionality in C indirectly. One of the approach is as follows.
Have a void * type of pointer as an argument to the function. And another argument telling the actual data type of the first argument that is being passed.
int foo(void * arg1, int arg2);
Suppose, arg2 can be interpreted as follows. 0 = Struct1 type variable, 1 = Struct2 type variable etc. Here Struct1 and Struct2 are user defined struct types.
While calling the function foo at different places…
foo(arg1, 0); /*Here, arg1 is pointer to struct type Struct1 variable*/ foo(arg1, 1); /*Here, arg1 is pointer to struct type Struct2 variable*/
Since the second argument of the foo keeps track the data type of the first type, inside the function foo, one can get the actual data type of the first argument by typecast accordingly. i.e. inside the foo function
There can be several other ways of implementing function overloading in C. But all of them will have to use pointers – the most powerful feature of C.
In fact, it is said that without using the pointers, one can’t use C efficiently & effectively in a real world program!
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Dangling, Void , Null and Wild Pointers
- Functions in C/C++
- C Language Introduction
- Implicit return type int in C
- What happens when a function is called before its declaration in C?
- Functions that are executed before and after main() in C
- return statement vs exit() in main()
- How to Count Variable Numbers of Arguments in C?
- exit(), abort() and assert()
- What is evaluation order of function parameters in C?
- Static functions in C
- Why C treats array parameters as pointers?
- What is the purpose of a function prototype?
- How can I return multiple values from a function?