xstrcat function

Discussion in 'C Programming' started by =?ISO-8859-1?Q?Leonardo_Javier_Bel=E9n?=, Jul 6, 2003.

  1. I've been studing the way to have a more useful strcat function and I
    realized that I need more than one argument to handle at a time, and
    also a way to resize the pointer automatically so I can forget the
    memory handling problem.

    So I constructed a new xstrcat function but, although works pretty
    well in most environments, I discover that it works erratically when I
    try to add the result var on the list of strings to add (i mean if I
    do a xstrcat(&tmp, tmp, NULL)), so I send this to the community to see
    if someone with more experience than me can help.
    Cheers,
    Leo

    char *
    xstrcat (char **dest, char *str, ...)
    {
    va_list va;
    size_t length = 0;
    char *ptr, *tmp;

    if (!str)
    return (char *) NULL;

    length += strlen (str);

    va_start (va, str);
    while ((tmp = va_arg (va, char *)))
    length += strlen (tmp);
    va_end (va);

    if (!*dest)
    *dest = malloc (length + 1);
    else
    *dest = realloc (*dest, (length + 1));

    if (!dest)
    return (char *) NULL;

    ptr = *dest;

    for (tmp = str; *tmp; tmp++)
    *ptr++ = *tmp;

    va_start (va, str);
    while ((tmp = va_arg (va, char *)))
    {
    while (*tmp)
    *ptr++ = *tmp++;
    }
    va_end (va);
    *ptr = '\0';
    return *dest;
    }
    =?ISO-8859-1?Q?Leonardo_Javier_Bel=E9n?=, Jul 6, 2003
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Leonardo_Javier_Bel=E9n?=

    Ben Pfaff Guest

    (Leonardo Javier Belén) writes:

    > So I constructed a new xstrcat function but, although works pretty
    > well in most environments, I discover that it works erratically when I
    > try to add the result var on the list of strings to add (i mean if I
    > do a xstrcat(&tmp, tmp, NULL)), so I send this to the community to see
    > if someone with more experience than me can help.


    > char *
    > xstrcat (char **dest, char *str, ...)
    > {
    > va_list va;
    > size_t length = 0;
    > char *ptr, *tmp;
    >
    > if (!str)
    > return (char *) NULL;


    The cast is gratuitous.

    > length += strlen (str);
    >
    > va_start (va, str);
    > while ((tmp = va_arg (va, char *)))
    > length += strlen (tmp);
    > va_end (va);
    >
    > if (!*dest)
    > *dest = malloc (length + 1);
    > else
    > *dest = realloc (*dest, (length + 1));


    1. realloc() acts like malloc() when the first argument is a null
    pointer, so there's no need to switch to malloc() for that
    case.

    2. If realloc() fails, you probably just leaked memory, because
    you overwrote the original pointer.

    > if (!dest)


    This does not test what you think it tests. If allocation
    failed, *dest will be a null pointer, not dest.

    > return (char *) NULL;


    Gratuitous cast.

    > ptr = *dest;
    >
    > for (tmp = str; *tmp; tmp++)
    > *ptr++ = *tmp;
    > va_start (va, str);
    > while ((tmp = va_arg (va, char *)))
    > {
    > while (*tmp)
    > *ptr++ = *tmp++;
    > }
    > va_end (va);
    > *ptr = '\0';


    The rest of this looks okay, though I'd prefer strcpy() followed
    by strchr() over explicit loops.

    > return *dest;
    > }


    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
    Ben Pfaff, Jul 7, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,014
    Evan Carew
    Jan 21, 2004
  2. komal
    Replies:
    6
    Views:
    1,409
    msalters
    Jan 25, 2005
  3. Replies:
    2
    Views:
    897
    Bengt Richter
    Aug 1, 2005
  4. Giannis Papadopoulos

    Function pointer to void function and int function

    Giannis Papadopoulos, Sep 5, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,218
    Barry Schwarz
    Sep 5, 2005
  5. weafon
    Replies:
    1
    Views:
    291
    Diez B. Roggisch
    Jul 14, 2009
Loading...

Share This Page