asw@rlvd.UUCP (Antony Williams) (12/13/85)
In article <564@puff.UUCP> tom@puff.UUCP writes: >ok, guys, now i will admit that the below code is *not* kosher. ^^^^^^ I don't know what you mean by kosher, but it is clear to me that in no way is the code defined to mean anything. >but the question still remains, if you run this program, what will >your output be? does the machine you compile it on make >a difference? does defining ARG to be something, say 999, ^^^^^^^^^^ ^^^^^^^^^^^^ you bet. no point >make a difference? what if VAR were auto or static? ^^^^^^^^^^^^^^ entirely irrelevant. > >------------------------------------------------------------- ># define ARG ># define VAR register ># define CALL(x) (*(int (*)()) (x))(ARG) > >main() { > VAR thing = 0; >stuff: > printf("here it goes, thing is %d\n",thing); > if (!thing++) > CALL(stuff); > printf("one there it went, thing is %d\n",thing); > printf("two there it went, thing is still %d\n",thing); >} > > >/* lint outputs >test.c: >test.c(8): warning: questionable conversion of function pointer >*/ >------------------------------------------------------------- > >note the lint output. no kidding. what does the compiler do? Any compiler worth its salt should refuse to generate anything executable. This is C code, not assembler. >on a vax, (description of what various machines do) >------------------------------------------------------------- Anything you get is what the program deserves. Nowhere have I seen any statement to the effect that labels can be used for anything other than simple goto's. Most of the computing community tends to use labels with discretion, if at all. They are not normally considered to be fair game as values of variables. > > >i haven't been able to figure out anyway to "goto" a label that >i don't know. for example, i would like to do this: ^^^^^^^^^^ If you don't know where it is going, how do you know that the program is going top behave the way you want (only mildly sarcastic :-) > >------------------------------------------------------------- >main() { > int (*jump[])() = { l1,l2,l3,l4,l5,l6,l7,l8 } > > l1: /* code */ > l2: /* code */ > l3: /* code */ > l4: /* code */ > l5: /* code */ > l6: /* code */ > l7: /* code */ > l8: /* code */ > > goto *jump[whatever] >} the right way to do this is switch(whatever) { 0: /* code for l1 */ 1: /* code for l2 */ etc } if you want to do it twice, put a loop around it, with the appropriate values of "whatever". >------------------------------------------------------------- > >aside from the forward-referencing problem of the unseen labels, >this is a still syntax error. anyone have any way to do this? I can only assume you are a refugee from FORTRAN, where such things used to be normal. If you ever write such a program in C, please keep it to yourself. Even Fortran is beginning to see the error of its ways, and "deprecating" such usage. -- --------------------------------------------------------------------------- Tony Williams |Informatics Division UK JANET: asw@uk.ac.rl.vd |Rutherford Appleton Lab Usenet: {... | mcvax}!ukc!rlvd!asw |Chilton, Didcot ARPAnet: asw%rl.vd@ucl-cs.arpa |Oxon OX11 0QX, UK
thorinn@diku.UUCP (Lars Henrik Mathiesen) (12/21/85)
In article <564@puff.UUCP> tom@puff.UUCP writes: [describes code using a label as a function address, and the various results on different machines] Well, ON A VAX, all you have to do is to put in the register save mask (well, it was you who started doing assemblerish things in C :-). ------------------------------------------------------- main() { register thing = 0; if (0) { /* skip mask on first entry */ stuff: ; asm(".word 0x800"); /* save r11 (thing) */ ; } printf("Here it goes, thing is %d\n", thing); if (!thing++) (*(int(*)())stuff)(); printf("There it went (one), thing is %d\n", thing); printf("There it went (two), thing is still %d\n", thing); } ------------------------------------------------------- The results are relatively unsurprising: ------------------------------------------------------- Here it goes, thing is 0 Here it goes, thing is 1 There it went (one), thing is 2 There it went (two), thing is still 2 There it went (one), thing is 1 There it went (two), thing is still 1 ------------------------------------------------------- Remarks: R11 is saved at label stuff because that's what C does at entry to main; if you don't save it, the two last lines read 2 instead. Don't use cc -O, as this gets you an undefined label in `as'. Maybe this shows that the conceptual model behind C looks like a VAX; but then maybe we knew that already. -- Lars Mathiesen, DIKU, Copenhagen, Denmark ..!mcvax!diku!thorinn