I was thinking more like this;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_LEN 1000
int main(int argc, char ** argv)
{
char buffer[BUFFER_LEN];
int i;
memset(buffer,0,BUFFER_LEN);
for (i=1;i<=argc;i++)
strcat(buffer,argv[i]);
return 0;
}
As for zeroing the buffer.... Strings are not a data type in c. They
are, as noted elsewhere, arrays of chars that happen to have the last
character as zero. I try to remember that zero is the only thing that
differentiates an array of chars we call a string, from an array of any
other integral data types. I (because I think I'm cool, not necessarily
bright) tend to do silly things like ( while(*c)){ c++) ) Long before I
coded in constrained memory spaces (Windows, OS/2) I coded in the MSDOS
and embedded micro world and many times wrote out into the nether world
of OS/BIOS code! And once caused some really ugly things to happen to
my poor little XT's 10 mg hard drive.
And. I kinda think that we seem to be training up and coming code
cowboys here so we might, perhaps, maybe, ought to teach them good
programming practices, (someday they might be working for us!) not just
the cool tricky things that we all can to with 'C' (which, of course,
are the things that make me keep coming back to it when I'm not in the
C# or (gag again) VB.NET world). We could, I suppose, just point them
to the DR. Dobbs obfuscated C contest winners and really mess with
their heads!
anyway, 'tis better to be zeroed and sure, than to never have zeroed at
all.....
For Sale: Herd of Cows, never bred. Also one gay bull.
Michael Comperchio
[email protected]
On Mar 6, 2009, at 3:49 AM, John Matthews wrote:
> --- In [email protected], Michael Comperchio <mcmp...@...> wrote:
> >
> > he did say he was accepting strings....
> > can't accept a string without a second buffer :)
>
> #include <stdio.h>
> #include <string.h>
>
> #define BUFSZ 1000
>
> int main(void)
> {
> char buffer[BUFSZ], *s;
> size_t len;
>
> for (*(s = buffer) = '\0'; fgets(s, BUFSZ - (s - buffer), stdin); s
> += len) {
> /* Remove trailing \n added by fgets(). */
> if (((len = strlen(s)) > 0) && (s[len - 1] == '\n'))
> {
> s[--len] = '\0';
> }
> }
>
> printf("buffer=\"%s\"\n", buffer);
>
> return 0;
> }
>
> Run the program and enter:
>
> one
> two
> three
> <ctrl-D> or whatever to indicate EOF
>
> buffer="onetwothree"
>
>