R
Richard Heathfield
Peter said:MSG said:Yes, but others failed it many times as well, sometimes thanks to him [ERT],
e.g. when many C people stated that you could not return structs from
functions. I don't want to google, but it went something like this
struct foo f() {
struct foo s;
s.x = x;
s.y = y;
s.z = z;
return s;
}
*Many* (but not ERT) said you could not do this in C. So let's be fair
here.
OK, let's be fair. The code above *is* in fact illegal ;-)
Why?
$ cat foo.c
#include <stdio.h>
struct foo
{
int x;
int y;
int z;
};
int x, y, z;
/* Here's that code again, this time in some context. */
struct foo f() {
struct foo s;
s.x = x;
s.y = y;
s.z = z;
return s;
}
int main(void)
{
struct foo v;
x = 6;
y = 42;
z = 117;
v = f();
printf("%d\n%d\n%d\n", v.x, v.y, v.z);
return 0;
}
$ gcc -g -W -Wall -ansi -pedantic -o foo foo.c
$ ./foo
6
42
117
The fact that the code produces no diagnostics at a fairly severe warning
level, and gives the expected results, is of course not conclusive. But
neither is a simple assertion that the code is illegal!