|
I have a
char* a ="a sentence" . I need to copy this into another char* b , after allocating enough memory to b.
I am very unsure about how to get the length of a so I can do
char *b = new char[a_length+1];
If I use strlen , how exactly do I code this?
a_length = strlen(*a);
or
a_length = strlen(a);</
Appreciate your help,
ns
|
|
|
|
|
|
char *a="a sentence\0";
char *b = new char[strlen(a)];
make sure your string "a" is null-terminated. I don't know whether this is necessary, though
modified 12-Sep-18 21:01pm.
|
|
|
|
|
That sounds good too. Though I am wondering if the way you have definred the string it now has two NULLS at the end....?
Appreciate your help,
ns
|
|
|
|
|
Gregor's code is wrong, Nish already gave you the correct answer. Use strlen() to count the characters, then add 1 for the terminating 0 character.
--Mike--
Just released - 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click!
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
|
But the string needs to be null-terminated, or am I wrong?
He could also write char a[] = "a sentence";
Now you have confused me totally
modified 12-Sep-18 21:01pm.
|
|
|
|
|
just by doing char* pcChar = "";
u have a null terminated string, u usualy dont add \0 to the string but in special cases, like a filter for a CFileDialog for instance
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
Oh my god, it's really awkward I didn't know that
Sorry Michael, thanks Papa
regards
btw. nice signature you have there, is Bebe your wife?
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Glad i could help no not my wife
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
Gregor S. wrote:
char *a="a sentence\0";
You don't need the extra \0
Gregor S. wrote:
char *b = new char[strlen(a)];
This is the wrong way. Allways alloc +1 byte for the \0
char *b = new char[strlen(a)+1];
"In an organization, each person rises to the level of his own incompetence." Peter's Principle
|
|
|
|
|
I need to convert a CString into a char*. If the CString is called fldName , then is the following allowed?
char* fldName0 = new char [fldName.GetLength()+1];
strcpy(fldName0, fldName);
The definition of strcpy has two char* as arguments, so I am wondering....
Appreciate your help,
ns
|
|
|
|
|
|
Thank you.
Appreciate your help,
ns
|
|
|
|
|
Nishant S wrote:
(LPTSTR)s
NO NO NO. Sorry, I get upset when I see people do that. That is totally wrong and a bad habit to get newbie programmers into.
First off, the second strcpy() argument is a const char* (I'm avoiding the TCHAR typenames because ns obviously doesn't know them). There is no cast needed because CString has a const char* conversion operator that is called implicitly.
Second, casting a CString to a char* breaks the CString interface -- you are using your knowledge of how a CString works internally, which you must not do to maintain OO encapsulation. The correct way to get a non-const pointer is GetBuffer().
--Mike--
Just released - 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click!
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
Michael Dunn wrote:
The correct way to get a non-const pointer is GetBuffer().
Yeah, but ns only wanted a const char *.
To fiddle around with GetBuffer() is unnecessary here, why force CString to allocate new memory when he's only reading it?
But, more to the point, the whole idea of "converting" the CString to char * is a bit fruitless. You can interchange CString operator char * and CString::GetBuffer entirely for read and write operations that need char *, and this will be quicker than what amounts to doing everything twice, not to mention obfuscates the code.
Jon
Signature space for rent. Apply by email to....
|
|
|
|
|
oops. i only part read your answer properly. ignore the bits that make no sense
Signature space for rent. Apply by email to....
|
|
|
|
|
Michael Dunn wrote:
NO NO NO. Sorry, I get upset when I see people do that. That is totally wrong and a bad habit to get newbie programmers into.
First off, the second strcpy() argument is a const char* (I'm avoiding the TCHAR typenames because ns obviously doesn't know them). There is no cast needed because CString has a const char* conversion operator that is called implicitly.
Second, casting a CString to a char* breaks the CString interface -- you are using your knowledge of how a CString works internally, which you must not do to maintain OO encapsulation. The correct way to get a non-const pointer is GetBuffer().
-- The more I learn, the more I realize how little I know
Thanks Mike, I always learn something from your posts, even if they aren't my questions!
Nitron
_________________________________________--
message sent on 100% recycled electrons.
|
|
|
|
|
Michael Dunn wrote:
First off, the second strcpy() argument is a const char*
Oops! When ns mentioned that strcpy takes two char*s I sorta got absent minded about it
Michael Dunn wrote:
Second, casting a CString to a char* breaks the CString interface
Agreed! But here he only wanted a read-only char*. But I agree that it goes against OOP.
Regards,
Nish
p.s. Never on sonork eh? No beer in LA?
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Review by Shog9
Click here for review[NW]
|
|
|
|
|
In an attempt to clarify and emphasize Michael's response...
The explicit and gratuitous cast than Nish suggests is very bad practice. The original poster's code was entirely correct.
As Michael said, CString has a conversion operator to LPCTSTR (aka "const char *"). The second argument to strcpy is a const char *, not a char *, so the implicit conversion occurs and everybody's happy.
The gratuitous cast that Nish suggested is unnecessary and could later hide errors introduced by subsequent changes to the code. NEVER USE EXPLICIT CASTS UNLESS YOU ABSOLUTELY MUST!
I might add that the whole idea of allocating this buffer to a dumb pointer and then copying the string into it is quite questionable. We'd need more context to know whether or not there is any justification for it.
|
|
|
|
|
JT Anderson wrote:
explicit and gratuitous
*Nish staggers on to the ground, first Mike's dagger had pierced his chest and now JT's sword had cut his body into two, and bleeds to death*
But seriously thanks, I do have this bad habit. I have a lot of non-MFC code where I have a lot of functions that take char*s and when I reuse them from my MFC code I do make explicit casts from CString to char* (only when I am sure the char*s are read-only as far as the function is concerned, in which case I should actually have declared the arguments as const char* which I didn't do - another bad habit of mine)
But it is a real nasty bad habit and I better get myself out of it
Thanks fellas
Nish
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Review by Shog9
Click here for review[NW]
|
|
|
|
|
I think I should write an article "How to Convert CStrings Properly"
David Wulff:BT::Me:casting CStrings
--Mike--
Just released - 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click!
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
Maybe an article about different type of strings like char*, TCHAR, CString,std::string, etc. that would be great.
|
|
|
|
|
Actually, I am working on an article about string types and string classes, but I haven't touched it in about a week. I should finish that up since the frequency of string questions (especially about COM and BSTRs) seems to be going up.
--Mike--
Just released - 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click!
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
Cool, i`ll be waiting for it.
|
|
|
|