[C++]How to concatenate const char * , two a dinamyc char *?
2009-05-14 18:37:31 UTC
Is there any function two concatenate example:
const char * s= "ABC";
const char * d = "DEF";
in to one dynamic char array
in C++
I know I could use string, but is only for learning
Four answers:
Ngo Cong Huan
2009-05-14 18:45:01 UTC
using char *strcat(const char* s1, const char*s2) will give you the concatenated of s1 appends s2.
?
2016-10-22 09:39:45 UTC
there is not any longer realy a decimal form, there are training that supply what seems to be like decimal, yet those are no longer realy primary. the form mybe int double elect the flow char* Microsoft do a 'Decimal' class which would be used as though a double or elect the flow, that's extremely lots slower implementation than 'elect the flow', it somewhat is simply by fact it somewhat is not any longer a format this is properly matched with the math processor on your computing device's CPU, that's a utility implementation. Decimal is extra precise however, so if working time isn;t a topic it may grant extra precise outcomes. you ought to undergo in strategies however that no longer all C++ (and derived languages) could have the Decimal class, and in the event that they do they won't enforce it interior the comparable way, in actuality you will locate some are realy floats decrease than a distinctive call (like the 'forex' form generally is). So your decimal will not extra healthful right into a char, decimal is a 128bit fee, your char is 8bit. I basically had a glance, the link to the Decimal class is decrease than, it does have a ToByte and ToSByte (signed), in spite of the undeniable fact that it seems very dodgy to me, determine which you wrap those interior the try-seize as interior the occasion and shop an eye fixed on those exceptions to your form levels.
The Phlebob
2009-05-14 18:46:36 UTC
C's char * is very primitive compared to current C++ string types. strcat() will concatenate two strings, but it won't handle dynamic length expansion. You have to do that by hand, usually by using calloc() with the calculated combined length. Don't forget to do a free() of the old string's memory, if needed, and of the new one later in the program.
Hope that helps.
GunsBoy
2009-05-15 00:09:42 UTC
first you need to allocate size:
char* mystr;
mystr = new char [strlen(s)+strlen(d)+1];
from here you have about as many options as you want all depending on what C++ utilities you want to use.
One option, you can use a pointer to go through each character of s and d and copy content of the pointer to mystr.
Or just use [ ] to get the characters.
- Then do not forget to terminate your new string with the character '\0'
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.