James Kuyper said:
On 03/25/2014 01:03 PM, BartC wrote:
In any context where FILE is unknown, it would be equally impossible to
either declare "FILE *f", or to use the (FILE*) cast. In any context
that is sufficiently C-like, knowing FILE well enough to allow the
declaration f as FILE* renders it unnecessary to cast f to (FILE*).
Note: in C, writing such code does not require that "FILE" be a complete
type - it could, for instance, be a forward-declared struct, with no
definition of the struct type itself.
Therefore, there must be additional differences between the language
you're actually using, and C, beyond the ones you've mentioned above, in
order to justify writing such code.
(Because a file handle is usually a FILE* type, ie. a pointer, then from
outside of C, any pointer to anything, or even any int of the right width,
will generally do. However, because C source code is still involved, it
causes problems.
The actual situation is a bit complex, but to try and explain, there are
three languages, let's call them:
A - An independent language
B - A hybrid which is a thin wrapper around C
C - Raw C itself
If A was translated to assembly or direct to native code, there wouldn't be
a problem - I would just need to know whether a FILE* argument was 32 bits
or 64-bits wide (on my likely range of platforms). The type mismatch with an
actual FILE* wouldn't get in the way as there is no C compiler involved.
But A at the moment generates C source code, and A knows nothing about a
FILE type, so it is hard for A to directly call fopen() for example. So some
wrapper functions are created, which I suppose could have been written in C,
but I used B which has access to the same headers. This wrapper takes
something like an int* type and casts it to FILE*. The wrapper is called
from A as a foreign function, with parameter types it understands.
The variadic functions that use FILE* are an extra detail. Although A has
its own arrangements for i/o, it's convenient to be able to call these from
A too (porting existing code using -printf() functions for example).)