By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

concatenate strings

P: n/a
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.
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Lars Tackmann wrote:

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

--
pete
Nov 14 '05 #2

P: n/a
Lars Tackmann <ro****@diku.dk> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@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)
Nov 14 '05 #3

P: n/a
On 2003-12-30, Keith Thompson <ks***@mib.org> wrote:
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.

--
Stefan Farfeleder
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.