开发者

Can I declare a function without specifying a type for one of the parameters?

Can a function be defined like this:

int foo(int temp1, in开发者_StackOverflow社区t temp2 temp3) {
   ...
}

Specifically with temp2 and temp3, will that cause errors? If not, what's the overall effect?


You're all wrong.. This is perfectly valid with:

#define temp2 blah) { return 1; } int foo_ (int
int foo(int temp1, int temp2 temp3)
{
        return 0;
}

(This is the outcome of me feeling a little humorous first thing in the morning - feel free to downvote if you'd like to ;))


An error is the overall effect.


No, that's not valid C.


If you're really trying to pass three arguments to a function but you only know the types of two of them at compile time then you can do it using a variable argument list. Suppose you want the third argument to be an int or double but you have to check temp1 or temp2 first to know which it should be:

#include <stdarg.h>

int foo(int temp1, int temp2, ...) {
    va_list ap;
    int     temp_int;
    double  temp_double;

    va_start(ap, temp2);
    /*
     * Figure out what type you want the third argument to be
     * and use va_arg(ap, int) or va_arg(ap, double) to pull
     * it off the stack.
     */
    va_end(ap);

    /*
     * Get on with what foo() is really all about (including
     * return an int.
     */
}

This sort of hack won't protect you against someone saying foo(1, 2), foo(3, 4, "fish"), or similar shenanigans but this is C and C assumes that you're grown up and responsible for your own actions.


No it cannot. This will not work because temp3 is not really an argument. The compiler will come up with an error.


if emp2 and temp3 are parameters so, each of them should have its own type, but this will provide a compilation error


What you have written is not how functions are called but rather how they are declared. You will need a datatype before each of the formal parameters (int temp1, int temp2, int temp3)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜