Function overloading in C

前端 未结 5 1968
花落未央
花落未央 2020-12-10 01:23

Today, looking at the man page for open(), I\'ve noticed this function is \'overloaded\':

   int open(const char *pathname, int flags);
   int o         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 01:40

    C does make it possible to write function with a variable number of argument, such as printf.

    With that being said, there is no reliable, cross-platform way in C to write a function that takes exactly 2 or 3 arguments; in general you must do something like

    some_function(5, 6, 7, NULL);
    some_function(5, 6, 8, 2, 5, NULL);
    

    In other words, you must have a terminating "sentinal" argument. Alternatively, you could include the number of parameters somehow in an earlier parameter, such as

    another_func(2, "hello", "world");
    another_func(3, "goodbye", "cruel", "world");
    

    The printf family of functions take this approach; the first format parameter contains the number of extra parameter needed; e.g. with printf("%f %f", 5.6, 7.11) you know that there must be 2 float parameters. However, this would be somewhat unsafe in a user-defined library function, since if you said my_printf("%s %f %f %f %s", 5.6) then you could get segfaults or worse. Fortunately, most C compilers will check your calls to printf at compile time to avoid this kind of issue.

    In the case of open, the function is declared as having variable arguments, and the third parameter is only checked if O_CREAT is set. So this is how it "safely" determines whether a third argument is present. I put "safely" in quotes because technically there's no way for open to know at runtime how many parameters were actually passed. For example, the following calls would compile without any errors or warnings:

    open("foo.txt", 5, "not an integer", 7);    // extra and invalid parameters
    open("bar.txt", O_CREAT);                   // third parameter is missing
    

提交回复
热议问题