Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!rpi!uupsi!cmcl2!kramden.acf.nyu.edu!brnstnd From: brnstnd@kramden.acf.nyu.edu (Dan Bernstein) Newsgroups: comp.lang.c Subject: Re: A quick question... Message-ID: <20125:Mar1209:03:3491@kramden.acf.nyu.edu> Date: 12 Mar 91 09:03:34 GMT References: <1991Mar12.030759.26698@nntp-server.caltech.edu> Organization: IR Lines: 37 In article <1991Mar12.030759.26698@nntp-server.caltech.edu> eychaner@suncub.bbso.caltech.edu writes: > unsigned char *pointer1; > short short_value; The value that ``pointer1'' refers to has not yet been defined. It's probably garbage. > ... > *((short *) pointer1) = short_value; If ... doesn't include any initialization of pointer1, this has undefined behavior. If ... initializes pointer1 to point to some memory location other than that occupied by a short, your code sequence has undefined behavior. If ... initializes pointer1 to point to the memory occupied by a short, then this will work. You can cast freely between char *, void *, and any (single) other pointer-to-object type. This would work: char *pointer1; short foo; short bar; ... pointer1 = (char *) &foo; *((short *) pointer1) = bar; This has the same effect as foo = bar. I believe unsigned char * works the same as char * for this purpose. > And does it do what I think it does, that is, assign short_value to the > storage pointed to by pointer1? Only if you've set pointer1 to point to some valid short storage. (Of course, you need to initialize short_value as well.) ---Dan