Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!spool.mu.edu!uunet!world!ksr!jfw From: jfw@ksr.com (John F. Woods) Newsgroups: comp.lang.c Subject: Re: increment casted void pointer -- ANSI? Message-ID: <2548@ksr.com> Date: 8 Mar 91 18:15:49 GMT References: <4142@rwthinf.UUCP> Sender: news@ksr.com Lines: 46 berg@marvin.e17.physik.tu-muenchen.de (Stephen R. van den Berg) writes: >Suppose I have the following (silly example, I know :-) program: >main(){static char s[]="abcd";void*p;int a,b; > p=s; > a=*(char*)p; > b=*++(char*)p; > *--(char*)p=b; > *++(char*)p=a; > puts(s); > return 0;} >Now, you tell me if this is ANSI or K&R or neither, Neither. The operand of ++ and -- must be an lvalue; the result of a cast is an rvalue. >how do I do it in ANSI then? Leaving in the "silliness" to make the conceptual changes more clear: main(){ static char s[]="abcd"; void*p; char *cp; /* added */ char a,b; /* changed */ p=s; cp = p; /* added; note that void pointers are not objects * you DO things with; they are typeless bags * for data-pointers which should be assigned to * a correctly-typed pointer for real work. */ a = *cp; /*changed*/ b = *++cp; /*changed*/ *--cp = b; /*changed*/ *++cp = a; /*changed*/ puts(s); return 0; } By the way, b = cp[0]; cp[0] = cp[1]; cp[1] = b; is not only clearer, but stands a good chance of running as fast or faster. Try compiling to assembly code and counting cycles. Array notation is NOT evil or inefficient.