Why?? (prog question)
josh.carroll at gmail.com
Mon Mar 30 20:08:58 PDT 2009
On Mon, Mar 30, 2009 at 10:57 PM, Gary Kline <kline at thought.org> 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 <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> main(int argc, char argv)
> char buf;
> if (argc == 1)
> printf("Usage: %s < file > newfile\n", argv);
> exit (-1);
> while (fgets(buf, sizeof buf, stdin) )
> if (*buf == '\n')
> fprintf(stdout, "\n\n");
> buf[strlen(buf)-1] = ' ';
> fputs(buf, stdout);
main should be:
int main(int argc, char **argv)
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
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
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.
More information about the freebsd-questions