Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 30 Mar 2009 23:08:57 -0400
From:      Josh Carroll <josh.carroll@gmail.com>
To:        Gary Kline <kline@thought.org>
Cc:        FreeBSD Mailing List <freebsd-questions@freebsd.org>
Subject:   Re: Why?? (prog question)
Message-ID:  <8cb6106e0903302008j5ab06a97odbd32fb68c1a404d@mail.gmail.com>
In-Reply-To: <20090331025726.GA10888@thought.org>
References:  <20090331025726.GA10888@thought.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Mar 30, 2009 at 10:57 PM, Gary Kline <kline@thought.org> wrote:
> people, i've been under the weather for days and will probably be for a f=
ew more.
> new =A0and TEMPORARY meds dont like me, ugh.
>
> can anybody clue me in why the followin joinline program fails to catch i=
f argc =3D=3D 1?
>
>
> /*
> =A0* simple prog to join all | very nearly all lines of a text file that
> =A0* make up one paragraph into one LONG line.
> =A0*
> =A0* paragraphs are delimiated by a single \n break.
> =A0*/
>
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
>
> main(int argc, char argv[])
> {
> =A0 char buf[65536];
>
> =A0 if (argc =3D=3D 1)
> =A0 {
> =A0 =A0 =A0 =A0printf("Usage: %s < file > newfile\n", argv[0]);
> =A0 =A0 =A0 =A0exit (-1);
> =A0 }
> =A0 while (fgets(buf, sizeof buf, stdin) )
> =A0 {
> =A0 =A0 if (*buf =3D=3D '\n')
> =A0 =A0 {
> =A0 =A0 =A0 fprintf(stdout, "\n\n");
> =A0 =A0 }
> =A0 =A0 else
> =A0 =A0 {
> =A0 =A0 =A0 buf[strlen(buf)-1] =3D ' ';
> =A0 =A0 =A0 fputs(buf, stdout);
> =A0 =A0 }
> =A0 }
> }

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 proper=
ly.

Note also that your main should have an int return type and should
return a value.

Regards,
Josh



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?8cb6106e0903302008j5ab06a97odbd32fb68c1a404d>