Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site harvard.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!wjh12!harvard!breuel From: breuel@harvard.ARPA (Thomas M. Breuel) Newsgroups: net.lang.c Subject: limitations of casts, pointer and function declarartions... Message-ID: <120@harvard.ARPA> Date: Sun, 28-Oct-84 03:56:08 EST Article-I.D.: harvard.120 Posted: Sun Oct 28 03:56:08 1984 Date-Received: Mon, 29-Oct-84 02:36:10 EST Distribution: net Organization: Aiken Computation Laboratory, Harvard Lines: 73 The following questions came up while I was implementing the kernel of a PROLOG pseudo-code interpreter in 'C'. I think that these three constructs, casting the lhs of an assignment, pointers to themselves, and functions with return value of a pointer to their own type, are very useful (say convenient), in particular for the implementation of symbol manipulation languages, and I hope that there are some nice cpp & ccom tricks around to make them more convenient, and I hope that they will eventually be permitted by the compiler. ============================================================================== The (4.1/4.2BSD) C-compiler does not accept statements of the following form: { int x; char *y; /*### [cc] illegal lhs of assignment operator = %%%*/ (char *)x = y; } I don't think that this error message is sensible, since statments like *((long *) 100) = 100; work. Bug, feature; explanation, or excuse? (the solution here is, of course, to write 'x = (int)y;', but can the compiler make this transformation without ambiguity in general?). ============================================================================== I would like to declare a pointer to a thing of its own kind, i.e. something of the form: typedef ref *ref; 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. I.e. expressions of the following type should be possible: { ref a,b; long c; *a = b; a = *b; c = (int)a; a = (ref)c; a++; } My first attempt was: typedef long base; /* change this to int type with size of pointer */ typedef base *ref; #define deref(thing) ((ref)(*thing)) This works fine if one does all dereferencing through 'deref', except that assignments still don't work quite right, since 'deref(thing)=thing;' still does not work, and the rhs has to be cast instead (i.e. '*thing = (base)thing'). ============================================================================== 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)(); which is useful to implement continuations without having to resort to machine language hacks. ============================================================================== Thomas M. Breuel breuel@harvard.arpa ...{genrad!wjh12,seismo}!harvard!breuel