Article View: comp.std.c
Article #145Re: Question about ANSI-C
From: Norman Diamond
Date: Tue, 11 Aug 1992 10:36
Date: Tue, 11 Aug 1992 10:36
47 lines
2469 bytes
2469 bytes
In article <1992Aug10.204928.14917@frog.CRDS.COM> miket@frog.CRDS.COM (vulture) writes:
>According to my current book which may be outdated (if so, please offer
>better books to use)
For questions involving a standard, a better book would be the standard
itself. If you need a tutorial (textbook) first, then the standard isn't
the best.
>it says that the prototype 'void *' was introduced
>to ANSI C "to represent a 'universal' data pointer."
Any type of pointer-to-data can be converted to void* and back without
problems. However, if you try to hide the fact that conversion might
be occuring, then you can still get in trouble for it.
> typedef struct prot1 {
> int (*mycall)(char *parm1, void *parm_ptr );
> } PRO1;
The function pointed to by a "mycall" component must take a second parameter
of type void*. At the time of calling, you can put any kind of pointer in
the second parameter, and it will get converted to void*.
> static int myfunc1 ( char *myparm1, PRO2 *ptr1 ) {}
myfunc1() doesn't take a second parameter of type void*.
If you assign myfunc1 to the "mycall" component of some struct, then you
should expect a type mismatch because the parameter types are different.
WARNING: If you shut up the compiler by putting a cast in front of myfunc1,
so that you assign a compatible pointer-to-function type, then you should
expect random failures during execution (on some machines), because PRO2*
still isn't the same as void*, and you're preventing the conversion.
It's probably easiest to change myfunc1(), e.g.
static int myfunc1 ( char *myparm1, void *ptr0 ) {
PRO2 *ptr1 = ptr0;
/* ... */
}
> /* PRO2 not the same as PRO1 */
Irrelevant.
--
Norman Diamond diamond@jit081.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
"Yeah -- bad wiring. That was probably it. Very bad."
Technical Details
Message-ID:
<1992Aug11.103639.13307@nntpd.lkg.dec.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!haven.umd.edu!decuac!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamondReferences:
<1992Aug10.204928.14917@frog.CRDS.COM>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!sparky!uunet!haven.umd.edu!decuac!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamond From: diamond@jit345.bad.jit.dec.com (Norman Diamond) Subject: Re: Question about ANSI-C Message-ID: <1992Aug11.103639.13307@nntpd.lkg.dec.com> Sender: usenet@nntpd.lkg.dec.com (USENET News System) Reply-To: diamond@jit.dec.com (Norman Diamond) Organization: Digital Equipment Corporation Japan , Tokyo References: <1992Aug10.204928.14917@frog.CRDS.COM> Date: Tue, 11 Aug 1992 10:36:39 GMT Lines: 47