Okay, here's a toughie for you... (maybe)

Bob Martin rmartin at clear.com
Sun Dec 2 09:53:42 AEST 1990


In article <2784 at ux.acs.umn.edu> edh at 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 at 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.     |



More information about the Comp.lang.c mailing list