473,569 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10521
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_Keit h) 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.or g> 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_A RGS__, (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
26996
by: priya | last post by:
Hi How to concatenate two integer Values. Example Program : #include "Port.h" #include "BinaryConversion.h" # include "iostream.h"
13
2348
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 and so forth to the 1st string. Any ideas how to go about this? Thanks,
5
11239
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 must be the destination. How can I get arround?
14
27165
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++, and have the result as a char*. I first tried using strcat, but you need a const char* as the second parameter to work, but I don't know all the...
2
6670
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 into both fields 1 and 2. Here's the problem: It is possible that field1 may be blank (or may be made empty by the user). In this case, tabbing out...
6
1534
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
3543
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: -- ostringstream this does the job well, but: * all types involved have to support operator<< * we will lose some readibility in the code because we...
13
21800
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 will concatenate the value of ABC to MYSTR . I need this at compile time.
6
13533
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
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7677
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.