2

Is there a possibility to define function in C that depends on data type. I want to define a function which uses different procedures depending on 1d array or 2d array as a argument.

WoofDoggy
  • 269
  • 2
  • 8

4 Answers4

7

The easiest method these days would be to use a macro with _Generic. Here's an example showing how to use it.

#define cbrt(X) _Generic((X), \
          long double: cbrtl, \
              default: cbrt,  \
                float: cbrtf  \
)(X)

So, depending on the type, this macro will be replaced with a call to one of cbrtl(), cbrtf(), or cbrt().

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • I keep forgetting about `_Generic`, which is completely standard C99; probably because the sheer horror of it is causing memory cells to die. But it's incredibly useful and is definitely worth knowing about. (Although it's also worth pointing out that it's not in C++, and Microsoft's compilers don't do C99.) – David Given Feb 24 '15 at 19:18
  • @DavidGiven: If we're writing C++ code, then this whole discussion is moot. True templates exist in C++. – Bill Lynch Feb 24 '15 at 19:19
  • @mat69 Oops, you're absolutely right. C11 it is. – David Given Feb 24 '15 at 19:23
  • @BillLynch Because on Microsoft platforms people tend to compile C99 code using a C++ compiler, because C++ is enough like C99 that a lot of code works. The places where C++ is _not_ compatible with C99 are worth highlighting. (And this is not, in fact, in C99.) – David Given Feb 24 '15 at 19:26
0

C doesn't have overload capability, so in straight C, you will have to use 2 different functions.

If you are willing to go with C++, you could define the 1D array as a different data type then the 2D array, and then use overloading, allowing the C++ compiler to select the correct function, but not in straight C.

StarPilot
  • 2,246
  • 1
  • 16
  • 18
  • 1
    See http://stackoverflow.com/a/25026358/47453. The latest version of C can do this now. – amo Feb 24 '15 at 19:14
  • So not true overloading, but "close enough for government work" if you are using the latest C compliant compiler? Thanks for the tip. – StarPilot Feb 24 '15 at 19:17
0

Function overloading is not possible in C.

You still could implement something like this though using a variant type as outlined here. It can get messy fast though. So you are probably better off by implementing two functions.

Community
  • 1
  • 1
mfuchs
  • 2,190
  • 12
  • 20
0

Nope (at least not without _Generic shennanigans). Here's why.

Consider the following code:

int a(int b[][]) {
    return 0;
}

int a(int b[]) {
    return 0;
}

int main (int argc, char *argv[])
{
    int c = 5;
    a(c);
    return 0;
}

Now consider the output of the compiler (in this case gcc):

a.c:1:11: error: array type has incomplete element type
 int a(int b[][]) {
           ^
a.c:5:5: error: redefinition of ‘a’
 int a(int b[]) {
     ^
a.c:1:5: note: previous definition of ‘a’ was here
 int a(int b[][]) {
     ^
a.c: In function ‘main’:
a.c:12:5: warning: passing argument 1 of ‘a’ makes pointer from integer without a cast [enabled by default]
     a(c);
     ^
a.c:5:5: note: expected ‘int *’ but argument is of type ‘int’
 int a(int b[]) {
     ^

In the example, the first a() function takes a two-dimensional array (technically a double-pointer), and the second a() takes a single-dimensional array.

First off, the compiler won't allow it.

Second, what if I passed an integer to the function? the compiler prints a warning, but lets it happen and casts the integer to a pointer (single-dimensional array), but it wouldn't be able to tell whether to cast it as a single pointer (single dimensional array) or double pointer (two-dimensional array).

Community
  • 1
  • 1
djhaskin987
  • 9,741
  • 4
  • 50
  • 86