S
Schnoffos
Given two functions:
f (T1 a, T2 b, ... Tn z)
and
g (struct {T1 a; T2 b; ... Tn z} s)
i wonder how portable it is to call them (proper casts provided) using
the respecitve other signature, for example:
f( {a, b, .. z} );
g (a, b, ..z);
The question implies that I don't believe this is correct C at all,
but I wonder if there are compilers that crash on this.
Reasons for crashes could be different padding of structures and
argument lists, which I believe will seldom occure (if padding is used
for structures it will probably also be used for actual argument
lists). Another reason may be different ordering of arguments such
that for f(a,b,c) &a < &b is not necessarily true.
It seems probable to me, that though this is theoretically unportable
and simply invalid C code, it will in pratice work on most if not all
current compilers and platforms.
Is this correct? Can you provide me with examples where this does or
does not work?
I used the following code to test it:
--- snip ---
struct sii { char x; int y; };
void f_ii (char x, int y) { printf ("%c, %d\n", x, y); }
void f_sii (struct sii s){ printf ("%c, %d\n", s.x, s.y); }
void (*fp_ii)(char x, int y);
void (*fp_sii)(struct sii);
main ()
{
fp_ii = f_ii;
fp_ii ('1', 2);
fp_ii = (void (*)(char, int))f_sii;
fp_ii ('1', 2);
}
--- snip ---
Greetings & Thanks
PS: If you reply by mail, please replace utech.de with t-online.de
f (T1 a, T2 b, ... Tn z)
and
g (struct {T1 a; T2 b; ... Tn z} s)
i wonder how portable it is to call them (proper casts provided) using
the respecitve other signature, for example:
f( {a, b, .. z} );
g (a, b, ..z);
The question implies that I don't believe this is correct C at all,
but I wonder if there are compilers that crash on this.
Reasons for crashes could be different padding of structures and
argument lists, which I believe will seldom occure (if padding is used
for structures it will probably also be used for actual argument
lists). Another reason may be different ordering of arguments such
that for f(a,b,c) &a < &b is not necessarily true.
It seems probable to me, that though this is theoretically unportable
and simply invalid C code, it will in pratice work on most if not all
current compilers and platforms.
Is this correct? Can you provide me with examples where this does or
does not work?
I used the following code to test it:
--- snip ---
struct sii { char x; int y; };
void f_ii (char x, int y) { printf ("%c, %d\n", x, y); }
void f_sii (struct sii s){ printf ("%c, %d\n", s.x, s.y); }
void (*fp_ii)(char x, int y);
void (*fp_sii)(struct sii);
main ()
{
fp_ii = f_ii;
fp_ii ('1', 2);
fp_ii = (void (*)(char, int))f_sii;
fp_ii ('1', 2);
}
--- snip ---
Greetings & Thanks
PS: If you reply by mail, please replace utech.de with t-online.de