by saagarjha · 7y ago · 0 comments
But this would mean you no longer have a nice way of specifying a function that takes parameters that you can't list :(
int foo()
int x;
int y;
{ return x + y; }
Perfectly legal C, if not an old archaic and seldom used syntax. I saw it in some proprietary code, so, no, I cannot provide a link. It was odd, but worked for what it was trying to do.It pays to know the language you work in, not to write things like this, but to understand on the off chance you encounter it in the wild.
Edit, I may be wrong on the syntax, it might be:
int foo(x, y)
int x;
int y;
{ return x + y; }
I dont have the ISO spec in front of me, but one of these should work.Rather it should look like this:
int foo(x, y)
int x;
int y;
{ return x + y; }
x and y would be assumed to be int in the absence of the type specifiers, so in this case they're optional. foo(x, y) { return x + y; }If we stick to standard C, all calls are statically determined, so your only option is to switch among different call expressions, like:
switch (nargs) {
case 0: return f();
case 1: return f(a[0]);
case 2: return f(a[0], a[1]);
...
}
Here, if the f pointer is under-declared, it saves you some casting.You can use a union:
struct fun {
int nargs;
union {
int (*ptr0)(void);
int (*ptr1)(valtype);
int (*ptr2)(valtype, valtype);
...
} fn;
}
Then: switch (f->nargs) {
case 0: return f->fn.ptr0();
case 1: return f->fn.ptr1(arg[0]);
...
}
Now there is a modicum of type checking. When you create the "struct fun" object, you may be able to take the address of a function that is compatible with one of the ptr's, and assing it to the correct union member without having to use a cast. If you record the number of arguments correctly, it won't be misused.One of my coworkers could defeat any idea for a new optimization with "well, what about unions?" which would typically complicate things quickly...
Main takes argc and argv, but it's idiomatic to omit them if unused.
It works because with the way the C ABI works on all platforms I'm aware of, extra arguments passed to the function can be completely ignored by the function with no ill effects, so the fact that _start actually invokes main with argc and argv can be ignored by main.
Which is to say, you could also write this as
int main(void) { return 0; }
and that would work equally as well. int foo(int x, y, z, float a, b, c) { ...
instead of int foo(int x, int y, int z, float a, float b, float c) { ...
Yes, you can pack things like that into a struct sometimes, but not always. From talking to folks who were on the ANSI committee at the time, there was some reason why the first example wouldn't work. Some parsing ambiguity, but I forget the details.It's distressing to see new languages (like D) adopt this verbose style as if it were some well thought out idea. It wasn't, it was just a consequence of the new declaration style that the ANSI C committee adopted.
varargs functions require at least one non-vararg parameter.
(As int_19h said:) On some architectures, vararg functions have their own distinct ABI, so (...) as a declaration is not compatible with any definition that doesn't also use (...).
So perhaps foo(args) means foo takes a list of arguments unspecified.
result = foo(&(struct foo_args){.arg1 = 4, .arg7 = "2"})I think what I want is llvm's blocks. Which isn't available in gcc.