Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #157280

Re: Concatenate strings with '\0'

From Anton Shepelev <anton.txt@gmail.com>
Newsgroups comp.lang.c
Subject Re: Concatenate strings with '\0'
Date 2020-12-16 02:01 +0300
Organization A noiseless patient Spider
Message-ID <20201216020159.4139fc283b741fa1f199b83c@gmail.com> (permalink)
References <rr62do$ts4$1@dont-email.me> <HL8CH.176$iY1.113@fx39.iad>

Show all headers | View raw


Scott Lurndal:

> #include <stdio.h>
> #define MAX_LEN_STRING          2048
>
> int
> main(int argc, const char **argv)
> {
>     char buffer[MAX_LEN_STRING+1];
>     char *bp = buffer;
>     int arg = 1;
>     while ((arg < argc) && (bp < &buffer[MAX_LEN_STRING])) {
>         const char *cp = argv[arg++];
>         while ((bp < &buffer[MAX_LEN_STRING]) && *cp !='\0') *bp++ = *cp++;
>         if (bp < &buffer[MAX_LEN_STRING]) *bp++ = '\0';
>     }
>
>     fwrite(buffer, bp - buffer, 1, stdout);
>     return 0;
> }

What I dislike about this solution is the repetitive
checking for remaining buffer space. My version in compact
style, also 18 lines:

#include <stdio.h>
#include <stdlib.h>

#define N 8
int main( int argc, char** argv )
{  int   a, pos;
   char  buf[N], *src;

   if( argc == 1 ) return 1; /* no arguments */
   src = argv[1]; pos = 0; a = 1;
   while( 1 )
   {  buf[pos++] = *src;
      if( *src++ == '\0' )
      {  if( ++a == argc ) break;
         src = argv[a];  }
      if( pos == N ) return 2;  } /* insufficent buffer */
   fwrite( buf, pos, 1, stdout );
   return 0;  }

-- 
()  ascii ribbon campaign -- against html e-mail
/\  http://preview.tinyurl.com/qcy6mjc [archived]

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Concatenate strings with '\0' pozz <pozzugno@gmail.com> - 2020-12-13 22:51 +0100
  Re: Concatenate strings with '\0' Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-13 23:27 +0000
    Re: Concatenate strings with '\0' Vir Campestris <vir.campestris@invalid.invalid> - 2020-12-14 14:56 +0000
  Re: Concatenate strings with '\0' Bonita Montero <Bonita.Montero@gmail.com> - 2020-12-14 18:44 +0100
    Re: Concatenate strings with '\0' Sjouke Burry <burrynulnulfour@ppllaanneett.nnll> - 2020-12-14 19:38 +0100
      Re: Concatenate strings with '\0' Bonita Montero <Bonita.Montero@gmail.com> - 2020-12-14 19:41 +0100
        Re: Concatenate strings with '\0' Sjouke Burry <burrynulnulfour@ppllaanneett.nnll> - 2020-12-14 19:59 +0100
          Re: Concatenate strings with '\0' Bonita Montero <Bonita.Montero@gmail.com> - 2020-12-14 20:05 +0100
      Re: Concatenate strings with '\0' Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2020-12-14 19:50 -0700
    Re: Concatenate strings with '\0' Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-12-14 11:51 -0800
      Re: Concatenate strings with '\0' Bonita Montero <Bonita.Montero@gmail.com> - 2020-12-14 21:12 +0100
  Re: Concatenate strings with '\0' scott@slp53.sl.home (Scott Lurndal) - 2020-12-15 20:11 +0000
    Re: Concatenate strings with '\0' Anton Shepelev <anton.txt@gmail.com> - 2020-12-16 02:01 +0300
      Re: Concatenate strings with '\0' scott@slp53.sl.home (Scott Lurndal) - 2020-12-15 23:18 +0000
        Re: Concatenate strings with '\0' Bart <bc@freeuk.com> - 2020-12-16 00:04 +0000
        Re: Concatenate strings with '\0' Anton Shepelev <anton.txt@gmail.com> - 2020-12-16 12:34 +0300
          Re: Concatenate strings with '\0' Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-16 11:18 +0000
            Re: Concatenate strings with '\0' Anton Shepelev <anton.txt@gmail.com> - 2020-12-16 14:33 +0300
        Re: Concatenate strings with '\0' Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-12-19 15:35 +0000
      Re: Concatenate strings with '\0' Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-16 00:59 +0000
        Re: Concatenate strings with '\0' pozz <pozzugno@gmail.com> - 2020-12-16 08:45 +0100

csiph-web