Converting String to Char Array

A

AlexFarokhyans

Hello,
I'm trying to convert String to Char array. I'm getting a string from
user input text box and then I have char firstName[size].
I need to convert the string that is in the text box to
firstName[size].
Thank you
 
R

Ron Natalie

Hello,
I'm trying to convert String to Char array. I'm getting a string from
user input text box and then I have char firstName[size].
I need to convert the string that is in the text box to
firstName[size].
Thank you
strcpy(firstName, my_string.c_str());

provided you don't have any embedded nulls and you are sure firstName
is large enough.
 
A

AlexFarokhyans

if I use const char LastName[SC_PLYR_SIZEOF_LastName] here how I use
it:
strncpy( &headerbuf[SC_PLYR_OFFSET_LastName], (char *) LastName,
SC_PLYR_SIZEOF_LastName );
but now since i get the the Last Name from textbox as a String i'm not
sure how to do the strncpy
Thank you
 
H

Howard

if I use const char LastName[SC_PLYR_SIZEOF_LastName] here how I use
it:
strncpy( &headerbuf[SC_PLYR_OFFSET_LastName], (char *) LastName,
SC_PLYR_SIZEOF_LastName );
but now since i get the the Last Name from textbox as a String i'm not
sure how to do the strncpy
Thank you
What's a "String"? There is a class called "string" in the std namespace
(from <string>), but nothing called "String". If you're using MFC, maybe
you mean "CString"? In that case, check out an MFC newsgroup (or Google)
for answers concerning that class. If String is a class defined by some
thrid party, check the documentation for that class's library. You've
already been given the answer if you're using the std:string class.

-Howard
 
A

AlexFarokhyans

Thank you. I was able to fix the problem. Here is what I did:
String* lName = LastNameTexBox->Text;
char* pString =
(char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(lName).ToPointer();
 
H

Howard

Thank you. I was able to fix the problem. Here is what I did:
String* lName = LastNameTexBox->Text;
char* pString =
(char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(lName).ToPointer();

What the heck is that??? C#? It sure ain't anything from standard C++.
Methinks you're in the wrong newsgroup!
-Howard
 
I

Ioannis Vranos

Howard said:
What the heck is that??? C#? It sure ain't anything from standard C++.
Methinks you're in the wrong newsgroup!


That my friend is programming in .NET in the wrong way. You know bad
programming. There are many out there that do these kind of things.


Especially consider that System::String,(which contains wchar_t
characters, provides a method ToCharArray() that converts the String to
a wchar_t array.



Now try to figure out what the above thing of casts will result to, and
how guaranteed is that, that thing will work.
 
I

Ioannis Vranos

Ioannis Vranos wrote:

That my friend is programming in .NET in the wrong way. You know, bad
programming. There are many out there that do these kind of things.


I placed a comma in the above to make its meaning more obvious.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top