limitations of casts, pointer and function declarartions...

Doug Gwyn <gwyn> gwyn at brl-tgr.ARPA
Mon Oct 29 12:48:15 AEST 1984


> 	int x;
> 	char *y;
> /*### [cc] illegal lhs of assignment operator = %%%*/
> 	(char *)x = y;

You need an lvalue, not an expression, on the LHS.

> typedef ref *ref;

All types must reduce to a basic type (void, char, short, int, long,
float, double, struct/union) plus some number of operators (*, (), []).

> The point is that if a pointer of this type is dereferenced, it should
> have the same type again.  In addition, the type should be cast'able
> to/from integer, and the size associated with it should be that of a
> single pointer of its kind.

At present, the generic pointer type is (char *) (ANSI will probably
change this to (void *)).  A (char *) is castable to a (long) and back
without loss of information (note: NOT always to an (int)).  By using
appropriate type casts you can use the contents of a (char *) or (long)
for other purposes.

> Along the same lines, I'd like to be able to define a function
> returning a pointer to its own kind, i.e.
> 
> typedef fun (*fun)();

Ditto.

C is a typed language.  To implement another, untyped, language (or one
with types that do not reduce to basic types) in C you must pick a
definite C data type to represent objects in the other language (in this
case, ProLog).  Then you need to coerce data into the appropriate types
via typecasts when implementing recursive types etc.  C lets you do this
but you have to explicitly indicate that you are playing tricks with data
types.  (With some C implementations you can be pretty sloppy, but for
portable code follow the type rules and run everything past "lint".)



More information about the Comp.lang.c mailing list