Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!usc!apple!netcom!teda!ditka!mcdchg!tellab5!balr!clrcom!rmartin From: rmartin@clear.com (Bob Martin) Newsgroups: comp.lang.c Subject: Re: Okay, here's a toughie for you... (maybe) Message-ID: <1990Dec1.225342.21524@clear.com> Date: 1 Dec 90 22:53:42 GMT References: <2784@ux.acs.umn.edu> Organization: Clear Communications, Inc. Lines: 51 In article <2784@ux.acs.umn.edu> edh@ux.acs.umn.edu (Eric D. Hendrickson) writes: >The below program does not work. It seg faults in the last for loop. Can >someone see what is wrong here? I suspect I have made a mistake using the >pointers somewhere, but after much trial and error, little progress has >been made. Perhaps you can see what it is... > >thanks, > Eric >(btw -- if this is not a good question for comp.lang.c, please let me know) ^^^^^^^^^^^ I can't think of a better place.^^^^^^^^^^^ ======== WARNING WILL ROBINSON, EXTREME SOURCE CODE EDITTING.======= >/* extract the printcap setting requested */ >char ** >extract(grep) >char grep[]; >{ > char found[MAXPRINTERS][BUFSIZ]; /* holds found entries */ > strncpy((char *)found[j++], p, strlen(p)); > return((char **)found); >} > >int >chores(grep) >char *grep; >{ > static char **gots; > char **extract(); > > gots = (char **)extract(grep); > for( ; **gots != (char)'\0'; printf("%s\n", gots++)) ; >} > The crux of the problem is twofold. 1) the 'found' array is not static, and so won't exist when 'chores' want to print it. 2) the 'found' array cannot properly be cast to a char**. The reasons why this is true have been discussed profusely on the net in recent weeks and so I will not reiterate them here. -- +-Robert C. Martin-----+:RRR:::CCC:M:::::M:| Nobody is responsible for | | rmartin@clear.com |:R::R:C::::M:M:M:M:| my words but me. I want | | uunet!clrcom!rmartin |:RRR::C::::M::M::M:| all the credit, and all | +----------------------+:R::R::CCC:M:::::M:| the blame. So there. |