473,231 Members | 1,447 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,231 software developers and data experts.

char* concatenate

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 issues regarding const
chars, so maybe I overlooked something.

Then I've tried converting them to strings, and it works, but I can't
convert the result to a char* because c_str() gives me a const char*.
Or maybe I could take the value of the const char* somehow and plug it
in the char* result?

Thank you very much,
--Chris.

Oct 18 '06 #1
14 27121
me*********@gmail.com wrote:
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 issues regarding const
chars, so maybe I overlooked something.

Then I've tried converting them to strings, and it works, but I can't
convert the result to a char* because c_str() gives me a const char*.
Or maybe I could take the value of the const char* somehow and plug it
in the char* result?
The pointer returned by c_str() is indeed constant and you're not
supposed to modify it. If you really need a char* you have to copy the
pointed string into a char array first.

But why do you want a char*? Since you're a Java programmer you should
have no problems dealing with std::string instead of this old-style
C-string stuff. What is it exactly that you're trying to do?

Regards,
Bart.

Oct 18 '06 #2

"me*********@gmail.com писал(а):
"
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 issues regarding const
chars, so maybe I overlooked something.

Then I've tried converting them to strings, and it works, but I can't
convert the result to a char* because c_str() gives me a const char*.
Or maybe I could take the value of the const char* somehow and plug it
in the char* result?

Thank you very much,
--Chris.
Hi, Chris.
Maybe you have problems with strcat because your strings in char* are
not terminated by '\0'?

Oct 18 '06 #3
Hi,
But why do you want a char*? Since you're a Java programmer you should
have no problems dealing with std::string instead of this old-style
C-string stuff. What is it exactly that you're trying to do?
Alas, as with any programming tasks, I depend on the other programmers
in the project.
And in this case, this boils down to having to return a char*.

But you gave me a good point -- I'll simply use string, and modify a
few other lines in the project!

Thanks,
Chris.

Oct 18 '06 #4
Hi, Chris.
Maybe you have problems with strcat because your strings in char* are
not terminated by '\0'?
I'm sorry, I'm afraid my phrasing wasn't very clear -- strcat worked,
but I needed a const char* as second argument and I only had char*, and
I didn't know how to make a proper conversion.

Thank you for your insight!
--Chris.

Oct 18 '06 #5
metamorp...@gmail.com wrote:
Hi, Chris.
Maybe you have problems with strcat because your strings in char* are
not terminated by '\0'?
I'm sorry, I'm afraid my phrasing wasn't very clear -- strcat worked,
but I needed a const char* as second argument and I only had char*, and
I didn't know how to make a proper conversion.
char* gets converted to const char* implicitly. It's the other way
around (const char* to char*) that's more problematic. The const is
just there to tell you that the function promises not to modify the
argument.

Regards,
Bart.

Oct 18 '06 #6
me*********@gmail.com wrote:
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*.
The string type in C++ is called string.
char* is a pointer to a single character.
Oct 18 '06 #7
me*********@gmail.com wrote:
>Hi, Chris.
Maybe you have problems with strcat because your strings in char* are
not terminated by '\0'?
I'm sorry, I'm afraid my phrasing wasn't very clear -- strcat worked,
but I needed a const char* as second argument and I only had char*, and
I didn't know how to make a proper conversion.

Thank you for your insight!
--Chris.
char* converts to const char* implicitly.
Oct 18 '06 #8
Ron Natalie wrote:
me*********@gmail.com wrote:
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*.
The string type in C++ is called string.
I think the OP understands that, as he even mentioned it in his post.
char* is a pointer to a single character.
But it is commonly interpreted as a pointer to the first element of a
null-terminated array of characters. You have to be really pedantic to
interpret "concatenate multiple char*" as meaning anything else than
C-style string concatenation.

Regards,
Bart.

Oct 18 '06 #9
Solved!! Thank you all! :)
Back to Java with me now :D

--Chris.

Oct 18 '06 #10
Bart wrote:
Ron Natalie wrote:
>me*********@gmail.com wrote:
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*.
The string type in C++ is called string.

I think the OP understands that, as he even mentioned it in his post.
>char* is a pointer to a single character.

But it is commonly interpreted as a pointer to the first element of a
null-terminated array of characters. You have to be really pedantic to
interpret "concatenate multiple char*" as meaning anything else than
C-style string concatenation.
Maybe, but many programmers, especially those that are new to C or C++,
think that char* is supposed to be the string type. Phrases
like "concatenate multiple char*" are often (maybe not in this case, but
still often) an indication for such confusion. So it's a good idea to
mention clearly what char* actually is.

Oct 18 '06 #11
Metamorphiq posted:
I'd like to know how you concatenate multiple (4, in my case) char* in
C++, and have the result as a char*.
#include <cstddef>
#include <cassert>
#include <cstring>
#include <cstdlib>

#define restrict

char *const Con4Strs(char const *const restrict a,
char const *const restrict b,
char const *const restrict c,
char const *const restrict d)
{
assert(a); assert(b); assert(c); assert(d);

using std::size_t; using std::strlen; using std::memcpy;

size_t const lenA = strlen(a);
size_t const lenB = strlen(b);
size_t const lenC = strlen(c);
size_t const lenD = strlen(d);

char *const buf = new char[lenA+lenB+lenC+lenD+1];

register char *p = buf;

memcpy(p,a,lenA); p += lenA;
memcpy(p,b,lenB); p += lenB;
memcpy(p,c,lenC); p += lenC;
memcpy(p,d,lenD);

p[lenD] = 0;

return buf;
}

--

Frederick Gotham
Oct 18 '06 #12
<me*********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
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 issues regarding const
chars, so maybe I overlooked something.
If a function expects a const char* and you have a char* it works without
problem. It's going the other way that's a problem, I.E., you have a const
char* and the function expects a char*.

A const char* means that the function will not modify the contents of the
pointed to c-string used with c-strings. Which is good. You should be able
to use strcat without problems, although strcat has it's own problems.
Then I've tried converting them to strings, and it works, but I can't
convert the result to a char* because c_str() gives me a const char*.
Or maybe I could take the value of the const char* somehow and plug it
in the char* result?
The lifetime of a std::string.c_str() is only until the string goes out of
scope, or the string is modified somehow. You should not return a .c_str()
from a function. Return a std::string instead.
Thank you very much,
--Chris.
Even though you could get strcat to work for you, I would change everythign
to work with std::string and return a std::string, and modify the code that
calls the function to accept a std::string. Buffer problems just tend to go
away when you use std::string instead of c-style strings, especially for
what you are doing, modifying or returning a string inside a function.
Oct 18 '06 #13
Rolf Magnus wrote:
Bart wrote:
>Ron Natalie wrote:
>>me*********@gmail.com wrote:
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*.

The string type in C++ is called string.

I think the OP understands that, as he even mentioned it in his post.
>>char* is a pointer to a single character.

But it is commonly interpreted as a pointer to the first element of a
null-terminated array of characters. You have to be really pedantic to
interpret "concatenate multiple char*" as meaning anything else than
C-style string concatenation.

Maybe, but many programmers, especially those that are new to C or C++,
think that char* is supposed to be the string type.
Well, char* is a type that can be used to represent 0-terminated sequences
characters; and string operations modelled upon that representation are
provided in the header <cstring>.
Phrases
like "concatenate multiple char*" are often (maybe not in this case, but
still often) an indication for such confusion.
Which confusion? I think, those phrases only indicate that someone knows
that C-strings are supported in C++ and wants to use them. Sometimes the
reason fur such a wish might be that the person does not know about the
existence of std::string. But that is just a lack of knowledge, not a state
of confusion.
So it's a good idea to
mention clearly what char* actually is.
I think, a good idea is to just tell them about the existence of std::string
and to encourage its use.
Best

Kai-Uwe Bux
Oct 19 '06 #14
Kai-Uwe Bux wrote:
>>>The string type in C++ is called string.

I think the OP understands that, as he even mentioned it in his post.

char* is a pointer to a single character.

But it is commonly interpreted as a pointer to the first element of a
null-terminated array of characters. You have to be really pedantic to
interpret "concatenate multiple char*" as meaning anything else than
C-style string concatenation.

Maybe, but many programmers, especially those that are new to C or C++,
think that char* is supposed to be the string type.

Well, char* is a type that can be used to represent 0-terminated sequences
characters;
It can be (and often is) used to point to the first element of one. One
could say it can "represent" one. Still it's not a string type, and if you
try to use it like one, it won't work and might give surprising results.
and string operations modelled upon that representation are
provided in the header <cstring>.
>Phrases
like "concatenate multiple char*" are often (maybe not in this case, but
still often) an indication for such confusion.

Which confusion?
I have seen quite often that beginners don't understand why their "string
operations" don't work, including concatenating two char* with operator+ or
something like:

char* p = "Hello, world";
p[1] = 'X';

or maybe even:

char* p = "Hello, world, the number is: ";
char* q = p + 3;

which is actually correct, but obviously does something entirely different
from what was intended. I could probably give you a dozen other examples.
That is all just the result of confusing char* for a real string type. It's
important to know that C does not have a string type at all and that C++
provides the class std::string for this.
I think, those phrases only indicate that someone knows that C-strings are
supported in C++ and wants to use them. Sometimes the reason fur such a
wish might be that the person does not know about the existence of
std::string. But that is just a lack of knowledge, not a state of
confusion.
Well, confusion is mostly the result of a lack of knowledge, isn't it?
>So it's a good idea to mention clearly what char* actually is.

I think, a good idea is to just tell them about the existence of
std::string and to encourage its use.
That definitely too, yes.

Oct 19 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jean-michel | last post by:
I need to convert a decimal field to char() but also trim the leading zeroes. Any idea? I could not find any function to do that.
5
by: Abby | last post by:
Hi, I've got the following ... char user = "root"; unsigned long session = 0x0012453b; char result; How can I concatenate user and session to result? Thank you.
1
by: Martin Andersson | last post by:
Hi ------------------------------------------------------------- typedef struct bitarray { unsigned short length; /* number of significant bits */ unsigned char *value; } bitarray; ...
2
by: Poewood | last post by:
Okay I know this is basic C++ but can someone tell me how to concatenate char to string. Ex: char hexChar={'A','B','C','D','E','F'}; String *str ="A"; how can I do this: str = hexChar +...
10
by: tommaso.gastaldi | last post by:
I am extracting some field names from a table of a db (it's an OledbSchemaguid table). It occurs for some OleDbSchema driver that NameDBField = cstr(DataRow.Item(3)) return a string that is...
6
by: Sheldon | last post by:
Hi, I am trying to build a large array using concatenate function in python. So as I loop over the number of arrays, of which there are 12 (4 down and 3 across), I create 3 long arrays by...
6
by: tropos | last post by:
For my sins, I'm maintaining some old C code which is migrated to C++. Dozens of lines of it looks like this: char *cfd_THE_ts_result_sql= "select TRADE_DATE , VALUE , " " from (" " select...
11
by: mwebel | last post by:
Hi, i had this problem before (posted here and solved it then) now i have the same problem but more complicated and general... basically i want to store the adress of a istream in a char* among...
2
by: Oki | last post by:
I appreciate any help; I'm currently getting a problem with trying to concatenate a single char from a structure to an existing string. Basically, the problem is here: strcat( res, morse_c.letter...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

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.