Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!exodus!appserv!sun!amdcad!dgcad!dg-rtp!sheol!throopw From: throopw@sheol.UUCP (Wayne Throop) Newsgroups: comp.lang.c Subject: Re: A quick question Summary: suggested alternative... Message-ID: <1408@sheol.UUCP> Date: 18 Mar 91 00:54:04 GMT References: <1991Mar12.030759.26698@nntp-server.caltech.edu> <1991Mar12.062941.2369@nntp-server.caltech.edu> Lines: 62 > eychaner@suncub.bbso.caltech.edu (Amateurgrammer) > int do_array (void *array, int size, int type) > { > unsigned char *arrayptr; > int i; > > arrayptr = array; /* If only void * arithmetic were legal in VAX C */ > for (i = 0; i < size; i++) { > if (type == UNSIGNED_CHAR) { > /* My precedence rules say this is right */ > *arrayptr++ = some_value(); /* Declared properly elsewhere */ > } > else if (type == SHORT) { > /* My precedence rules ALSO say THIS is right */ > *(short *)arrayptr++ = othervalue(); /* Declared elsewhere */ > } > } > } Since what is wanted is a "generic" routine to operate on arrays of integers of various types (and since the common "wrapping" of this operation is large enough to want to commonize in a single routine), I'd try to make the binding of the void to specific type as localized and clear as possible. Tweaking the above example, I'd suggest: int do_array (void *formal_array, int size, int type) { int i; unsigned char *uchar_array = (unsigned char*)formal_array; short *short_array = (short*)formal_array; for (i = 0; i < size; i++) { if (type == UNSIGNED_CHAR) uc_array[i] = some_value(); else if (type == SHORT) short_array[i] = othervalue(); } } Further, if the persistence of the two "interpretations" of the "generic" argument didn't need to interpenetrate to make the loop control common, I'd segregate them, so that errors involving using the wrong interpretation of the formal would be less likely. For example: int do_array (void *formal_array, int size, int type) { int i; if (type == UNSIGNED_CHAR) { unsigned char *array = (unsigned char*)formal_array; for (i = 0; i < size; i++) array[i] = some_value(); } else if (type == SHORT) { short *array = (short*)formal_array; for (i = 0; i < size; i++) array[i] = othervalue(); } } There are further tricks that can be played with macros and so on (and more cleanly with by-name bindings in other languages), but in C these alternatives are mostly pretty disgusting in all but trivial examples. -- Wayne Throop ...!mcnc!dg-rtp!sheol!throopw