daniel@terra.ucsc.edu (Daniel Edelson) (04/02/91)
I have tried unsuccessfully to find an unambiguous statement.... in the standard that allows or prohibits the following cases: 1) a function declaration that defines a new type in the return type, and, 2) a function declaration that defines a new type in a parameter For example: /* * The return type of foo() defines a new type, struct S */ struct S { int a; } foo(void) { } /* * The parameter of bar() defines a new type, struct T */ void bar(struct T { int b; } p) { } The function ``bar'' is not particularly useful because the only type-safe calls to it can be recursive calls, since ``struct T'' has block scope. Are these strictly conformant ANSI C code? If so, in the above example, must ``struct S'' be given file scope? Thanks, Daniel Edelson
diamond@jit345.swstokyo.dec.com (Norman Diamond) (04/02/91)
In article <14000@darkstar.ucsc.edu> daniel@terra.ucsc.edu (Daniel Edelson) writes: >I have tried unsuccessfully to find an unambiguous statement.... >in the standard that allows or prohibits the following cases: > 1) a function declaration that defines a new > type in the return type, and, > 2) a function declaration that defines a new > type in a parameter Most of the allowed cases do not have specific statements for them. If all allowed cases were specifically described, the document would be larger than the universe. >For example: > /* The return type of foo() defines a new type, struct S */ > struct S { int a; } foo(void) { } > /* The parameter of bar() defines a new type, struct T */ > void bar(struct T { int b; } p) { } >Are these strictly conformant ANSI C code? Yes. >If so, in the above example, must ``struct S'' be given file scope? Since this is a DEFINITION (not just decaration) of foo, indeed it must be at file scope. Therefore the structure tag S has file scope. If the declaration were struct S { int a; } foo(void); then the scope of S would depend on where the declaration is located. >The function ``bar'' is not particularly >useful because the only type-safe calls to it can be >recursive calls, since ``struct T'' has block scope. If structural equivalence (rather than name equivalence) is the rule, then bar could be called with any struct that contains a single integer. It has been asserted in this newsgroup that structural equivalence is the rule within a single translation unit. I have not found a statement about this in the standard. (The rule for equivalence among separate translation units is neither of the above, and is specified.) -- Norman Diamond diamond@tkov50.enet.dec.com If this were the company's opinion, I wouldn't be allowed to post it.