Connecting Tech Pros Worldwide Forums | Help | Site Map

concatenate strings

Lars Tackmann
Guest
 
Posts: n/a
#1: Nov 14 '05
Hi - I need a function to concatenate a variable number of strings.
I have two ideas but cannot deside which way to go.

1) use the "stdarg" macros - if i use these macros it will be easy
to step through the strings, but as I see it (and I may be wrong here)I
need to pass the number of strings to the function - which is difficult in
this context.

2) using a pointer array as argument to the function. Here the length is
again a problem - the pointer array is passed on the stack so I cannot
use pointer arithmetic to calculate the length of the array. although
the final string will be created via malloc - i need a way to decide how
much memory to allocate on the heap.

did i miss any ?? way to do this - as I see it, I need to change my
program, so I always now how many strings I have - as i cannot decide on
the number og args to va_arg and i cannot calculate the length of my
pointer array.

I may be way of - so if anyone now a great method to do this, you
will save my day.

Thanks.

pete
Guest
 
Posts: n/a
#2: Nov 14 '05

re: concatenate strings


Lars Tackmann wrote:[color=blue]
>
> Hi - I need a function to concatenate a variable number of strings.
> I have two ideas but cannot deside which way to go.
>
> 1) use the "stdarg" macros - if i use these macros it will be easy
> to step through the strings, but as I see it (and I may be wrong here)I
> need to pass the number of strings to the function - which is difficult in
> this context.
>
> 2) using a pointer array as argument to the function. Here the length is
> again a problem - the pointer array is passed on the stack so I cannot
> use pointer arithmetic to calculate the length of the array. although
> the final string will be created via malloc
> - i need a way to decide how much memory to allocate on the heap.[/color]

You can NULL terminate an array of pointers.

/* BEGIN hello.c */

#include <stdio.h>

void function(char **);

int main(void)
{
char *array[] = {"hello, ", "world", NULL};

function(array);
putchar('\n');
return 0;
}

void function(char **strings)
{
while (*strings) {
fputs(*strings, stdout);
++strings;
}
}

/* END hello.c */

--
pete
Keith Thompson
Guest
 
Posts: n/a
#3: Nov 14 '05

re: concatenate strings


Lars Tackmann <roland@diku.dk> writes:[color=blue]
> Hi - I need a function to concatenate a variable number of strings.
> I have two ideas but cannot deside which way to go.
>
> 1) use the "stdarg" macros - if i use these macros it will be easy
> to step through the strings, but as I see it (and I may be wrong here)I
> need to pass the number of strings to the function - which is difficult in
> this context.
>
> 2) using a pointer array as argument to the function. Here the length is
> again a problem - the pointer array is passed on the stack so I cannot
> use pointer arithmetic to calculate the length of the array. although
> the final string will be created via malloc - i need a way to decide how
> much memory to allocate on the heap.[/color]

Pete pointed out that you can use a NULL pointer to terminate an
array; you can do the same with a stdarg argument list. A call might
look like this:

my_func("foo", "bar", "baz", (char*)NULL);

(Yes, the cast is necessary.)

Inside my_func(), you can traverse the argument list once to add up
the sizes, malloc() the needed memory, then traverse the list again to
copy the strings to the malloc()ed space.

On the other hand, why is passing the number of strings difficult? It
could be a maintenance headache, as you need to change the number if
you add or remove arguments, but the number is constant and you can
determine it at the call.

If you need to pass a number of arguments that's determined at
execution time, you probably need to build an array of pointers and
pass it.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Stefan Farfeleder
Guest
 
Posts: n/a
#4: Nov 14 '05

re: concatenate strings


On 2003-12-30, Keith Thompson <kst-u@mib.org> wrote:
[color=blue]
> Pete pointed out that you can use a NULL pointer to terminate an
> array; you can do the same with a stdarg argument list. A call might
> look like this:[/color]
[color=blue]
> my_func("foo", "bar", "baz", (char*)NULL);[/color]
[color=blue]
> (Yes, the cast is necessary.)[/color]

Using C99's macros with a variable number of arguments, you can even
define a convenience macro that allows you to omit NULL:

#define my_func(...) my_func2(__VA_ARGS__, (char *)NULL)

You have to pass one or more strings though.

--
Stefan Farfeleder
Closed Thread