473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

concatenate strings

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
3 10509
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: priya | last post by:
Hi How to concatenate two integer Values. Example Program : #include "Port.h" #include "BinaryConversion.h" # include "iostream.h"
13
by: jt | last post by:
I can't seem to find a way to concatenate strings that have nulls within the string. I have a string that I need another string that has nulls in it and what to append the 2nd string, 3 string...
5
by: Mr. Ken | last post by:
In my function, my main calls a function to save the data. In script-based languages like matlab, I can use multiple in strcat. However, in C++ it's not working since first argument of strcat...
14
by: metamorphiq | last post by:
Hello, I'm a Java programmer, so I'm probably asking a very simple question here, but I have trouble solving it :) I'd like to know how you concatenate multiple (4, in my case) char* in C++,...
2
by: exapplerep | last post by:
I've seen how to use VBA code to concatenate two fields into a third by using an expression in the "After Update" property in fields 1 & 2. field3 = field1 + field2 The above code would go...
6
by: Elamathi | last post by:
I just wanted to concatenate 3 strings addr1,addr2,addr3 and should be placed again in addr1 help me
5
by: Diego Martins | last post by:
Since C++ (and STL) have many ways to do string concatenation, I want to hear (read) from you how you do to concatenate strings with other strings and other types. The approaches I know are: --...
13
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
6
by: mark83anthony | last post by:
How do I concatenate strings from a column into a single row? Whats the logic to create the function in DB2. Given is below. Color ------ red orange
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.