Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!mit-eddie!ll-xn!ames!ptsfa!ihnp4!cuae2!ltuxa!ttrdc!levy From: levy@ttrdc.UUCP (Daniel R. Levy) Newsgroups: comp.lang.c Subject: Re: Perror (was Re: Accessing argc & argv from a functi) Message-ID: <1875@ttrdc.UUCP> Date: Mon, 24-Aug-87 03:59:25 EDT Article-I.D.: ttrdc.1875 Posted: Mon Aug 24 03:59:25 1987 Date-Received: Tue, 25-Aug-87 02:28:25 EDT References: <22@flmis06.ATT.COM> <28700015@ccvaxa> <853@mcgill-vision.UUCP> <518@sugar.UUCP> Organization: AT&T, Skokie, IL Lines: 47 Summary: picky point In article <518@sugar.UUCP>, peter@sugar.UUCP (Peter da Silva) writes: > You shouldn't pass null to perror. It's not guaranteed to work. It might > even crash you. > > It's easy enough to do this: > > extern int errno; > extern char **sys_errlst; /* Better: */ extern char *sys_errlist[]; ^ Note that "extern char **sys_errlst" would declare the location sys_errlst as containing a pointer to pointer to char, whereas "extern char *sys_errlist[]" declares the location sys_errlist as (more or less; purists will not agree) containing the first member of an array of pointer to char. The results you get from trying to interchange the two in an extern declaration are surprising, to say the least. > > fprintf(stderr, "%this, %that: %s\n", this, that, sys_errlst[errno]); Note too that errno is not guaranteed to lie within the valid range of sys_errlist. sys_errlist is defined from errno==0 to errno==sys_nerr-1. Perhaps a STRERR() macro would be helpful: extern int errno, sys_nerr; /* declare these at the top */ extern char *sys_errlist[]; /* of your module */ #define STRERR() ((errno >=0 && errno < sys_nerr) ? \ sys_errlist[errno] : "Unknown error") ... fprintf(stderr,"%this, %that: %s\n", this, that, STRERR()); ... > -- Peter da Silva `-_-' ...!seismo!soma!uhnix1!sugar!peter (I said, NO PHOTOS!) -- |------------Dan Levy------------| Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa, | an Engihacker @ | vax135}!ttrdc!ttrda!levy | AT&T Computer Systems Division | Disclaimer: i am not a Yvel Nad |--------Skokie, Illinois--------|