From owner-freebsd-questions@FreeBSD.ORG Tue Mar 31 04:42:20 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 74F561065675 for ; Tue, 31 Mar 2009 04:42:20 +0000 (UTC) (envelope-from kline@thought.org) Received: from aristotle.thought.org (aristotle.thought.org [209.180.213.210]) by mx1.freebsd.org (Postfix) with ESMTP id 2945C8FC1A for ; Tue, 31 Mar 2009 04:42:19 +0000 (UTC) (envelope-from kline@thought.org) Received: from thought.org (tao.thought.org [10.47.0.250]) (authenticated bits=0) by aristotle.thought.org (8.14.2/8.14.2) with ESMTP id n2V4hMdE051782; Mon, 30 Mar 2009 21:43:22 -0700 (PDT) (envelope-from kline@thought.org) Received: by thought.org (nbSMTP-1.00) for uid 1002 kline@thought.org; Mon, 30 Mar 2009 21:42:15 -0700 (PDT) Date: Mon, 30 Mar 2009 21:42:15 -0700 From: Gary Kline To: Josh Carroll Message-ID: <20090331044214.GA11129@thought.org> References: <20090331025726.GA10888@thought.org> <8cb6106e0903302008j5ab06a97odbd32fb68c1a404d@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <8cb6106e0903302008j5ab06a97odbd32fb68c1a404d@mail.gmail.com> User-Agent: Mutt/1.4.2.3i X-Organization: Thought Unlimited. Public service Unix since 1986. X-Of_Interest: With 22 years of service to the Unix community. X-Spam-Status: No, score=-4.4 required=3.6 tests=ALL_TRUSTED,BAYES_00 autolearn=ham version=3.2.3 X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on aristotle.thought.org Cc: FreeBSD Mailing List Subject: Re: Why?? (prog question) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 Mar 2009 04:42:20 -0000 On Mon, Mar 30, 2009 at 11:08:57PM -0400, Josh Carroll wrote: > On Mon, Mar 30, 2009 at 10:57 PM, Gary Kline wrote: > > people, i've been under the weather for days and will probably be for a few more. > > new  and TEMPORARY meds dont like me, ugh. > > > > can anybody clue me in why the followin joinline program fails to catch if argc == 1? > > > > > > /* > >  * simple prog to join all | very nearly all lines of a text file that > >  * make up one paragraph into one LONG line. > >  * > >  * paragraphs are delimiated by a single \n break. > >  */ > > > > #include > > #include > > #include > > > > main(int argc, char argv[]) > > { > >   char buf[65536]; > > > >   if (argc == 1) > >   { > >        printf("Usage: %s < file > newfile\n", argv[0]); > >        exit (-1); > >   } > >   while (fgets(buf, sizeof buf, stdin) ) > >   { > >     if (*buf == '\n') > >     { > >       fprintf(stdout, "\n\n"); > >     } > >     else > >     { > >       buf[strlen(buf)-1] = ' '; > >       fputs(buf, stdout); > >     } > >   } > > } > > main should be: > > int main(int argc, char **argv) > > or perhaps > > int main(int argc, char *argv[]) > > As is, you're defining int as char argv[] (e.g. char *) instead of char **. > > What will likely happen is you'll get a segmentation fault when you > try to run the program, since your printf format spec has %s, but > you're passing it a char. > > In fact, if you compile it with -Wall, you'll see the two problems > I've mentioned: > > t.c:13: warning: second argument of 'main' should be 'char **' > t.c: In function 'main': > t.c:20: warning: format '%s' expects type 'char *', but argument 2 has > type 'int' > > Change char argv[] to char *argv[] or char **argv and it should work properly. > > Note also that your main should have an int return type and should > return a value. > > Regards, > Josh you got it; as far as i know this is the first time that i've ever done the "char argv[]" instead of the "char *argv[]". thanks! gary :x > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org" -- Gary Kline kline@thought.org http://www.thought.org Public Service Unix http://jottings.thought.org http://transfinite.thought.org The 2.41a release of Jottings: http://jottings.thought.org/index.php