Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > char* concatenate

Reply
Thread Tools

char* concatenate

 
 
Rolf Magnus
Guest
Posts: n/a
 
      10-18-2006
Bart wrote:

> Ron Natalie wrote:
>> http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      10-18-2006
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
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      10-18-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-19-2006
Rolf Magnus wrote:

> Bart wrote:
>
>> Ron Natalie wrote:
>>> (E-Mail Removed) 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
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      10-19-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenate/De-Concatenate Carlos VHDL 10 10-24-2012 01:04 PM
Concatenate datalist variables with javascript and html Luis Esteban Valencia ASP .Net 0 01-20-2005 01:57 PM
Concatenate =?Utf-8?B?TWlrZSBNb29yZQ==?= ASP .Net 3 11-24-2004 07:05 PM
Concatenate Hyperlink with databinder Luis E Valencia ASP .Net 0 05-27-2004 01:06 PM
concatenate files recursively in win32 Mike James Perl 1 09-19-2003 03:51 PM



Advertisments