Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!utcsri!greg From: greg@utcsri.UUCP Newsgroups: comp.lang.c Subject: Re: how do I initialize a function pointer to be NULL ? Message-ID: <5389@utcsri.UUCP> Date: Sat, 12-Sep-87 22:48:39 EDT Article-I.D.: utcsri.5389 Posted: Sat Sep 12 22:48:39 1987 Date-Received: Sun, 13-Sep-87 08:37:24 EDT References: <197@tiger.Princeton.EDU> <27742@sun.uucp> <2236@emory.uucp> Reply-To: greg@utcsri.UUCP (Gregory Smith) Organization: CSRI, University of Toronto Lines: 67 Keywords: NULLFUNC Summary: In article <2236@emory.uucp> platt@emory.UUCP (Dan Platt) writes: >In article <27742@sun.uucp> guy@sun.uucp (Guy Harris) writes: >>> If p is declared as : >>> struct x >>> { int (*p)(); }; >>> Now I want to initialize a variable y which is a structure x. >>> struct x y = { NULLFUNC }; >>> The question is what should I put into the #define NhULLFUNC .... ? >>> (I am using Microsoft C 4.0). Thanks for your help.# >> >>Unless Microsoft C 4.0 is badly broken, "#define NULLFUNC 0" should suffice; >>the compiler knows enough to coerce the "0" to a pointer of the appropriate >>type, and should do so in this context if it claims to implement C. NULL >>should also suffice, if it is defined as 0 or (sigh) 0L. >>-- >> Guy Harris > >Actually, depending on the model (the IBM machines have a segmented >addressing scheme), NULL is defined as 0 if the memory is in small or >medium models, and is defined as 0L in compact and large models. Unfortunately for this 0L rubbish, function pointers differ in size from data pointers in compact and medium models. Thus it trips over its own feet, and is guaranteed to give you the wrong result for say, function_expecting_possibly_null_function_pointer( NULL ); >Since a module may need to be compiled several ways, rather than >checking the compile flags in the preprocessor, it's easiest to >just define it as #define NULLFUNC NULL, and including either > or (either having NULL defined) before the other >define. ( you don't need NULL defined when you #define SOMETHING NULL, by the way). It's best to use 0 and then cast that where appropriate (i.e. in function call parameters). Writing code that compiles under various memory muddles is really no different from writing code that compiles for various machines. We got along fine with #define NULL 0 for all kinds of machines. If a function prototype is available, the cast for a NULL function parameter is unnecessary, but I leave them in, at least until I feel I will always be able to get an ANSI compiler. I find it immensely useful to make the Turbo C compiler warn me if a function call is made with no prototype. I wish it could also warn me if the prototype is causing a cast that would not be made without the prototype ( as in Root2 = sqrt(2), which works with a prototype but not without ). > >As far as the function is concerned, it doesn't care what kind of >pointer is passed to it; it just passes an address. The declaration >in the function where the routine is passed defines what the >function is expected to do (whether the address is to be read/write, or >called). > >Dan Pagan tripe. A pointer is more than just an address (and an 'address' may well be more than 'just an address'.) Firstly you can't read or write through a function pointer, and you can't call through a data pointer. The two are effectively in separate spaces. The pragmatisms of C allow you declare a function as expecting an (int *) and then pass that function a function pointer, but then you are just being very Wrong. -- ---------------------------------------------------------------------- Greg Smith University of Toronto UUCP: ..utzoo!utcsri!greg Have vAX, will hack...