concatenate strings

L

Lars Tackmann

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.
 
P

pete

Lars said:
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.

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 */
 
K

Keith Thompson

Lars Tackmann said:
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.

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.
 
S

Stefan Farfeleder

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.)

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top