|
-
December 1st, 2006, 06:32 PM
#1
How to convert a string into an array of char?
Hi.
I want to convert a string into an array of chars but I have no idea how to do it. Someone please help.
-
December 1st, 2006, 06:47 PM
#2
Re: How to convert a string into an array of char?
The function is 'std::string::c_str()':
Code:
std::string myString = "This is my string!";
const char *pArrayOfChars = myString.c_str();
assert(pArrayOfChars[2] == 'i');
Viggy
-
December 1st, 2006, 06:54 PM
#3
Re: How to convert a string into an array of char?
It could be something like:
Code:
#include <string>
#include <iostream>
using namespace std;
#define SIZE 30
int main(void)
{
string sStr="Hi, World";
char sArr[SIZE];
memset(sArr, 0, SIZE); //Inicialize array
memcpy(sArr, sStr.c_str(), strlen(sStr.c_str())); //copy Array
cout << sArr << endl;
}
Albert.
-
December 1st, 2006, 06:55 PM
#4
Re: How to convert a string into an array of char?
Thanks viggy, but i do not fully understand it. Could you add comments on each line please?
-
December 1st, 2006, 07:09 PM
#5
Re: How to convert a string into an array of char?
No, there is no need for that. I solved it thanks to albert's advice ^^ thanks albert!
-
December 1st, 2006, 07:19 PM
#6
Re: How to convert a string into an array of char?
Or, if you don't need your own copy, I'd stick with Viggy's advice:
Code:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string sStr ="Hi, World";
const char* sArr = sStr.c_str();
cout << sArr << endl;
}
Jeff
-
December 2nd, 2006, 03:22 AM
#7
Re: How to convert a string into an array of char?
 Originally Posted by AlbertGM
Writing void like that is redundant in C++ and doesn't make it look good.
 Originally Posted by AlbertGM
Code:
memset(sArr, 0, SIZE); //Inicialize array
No need to initialize this way - setting the first character to '\0' is enough for C-style strings.
 Originally Posted by AlbertGM
Code:
memcpy(sArr, sStr.c_str(), strlen(sStr.c_str())); //copy Array
Prone to going beyond sArr buffer size. Either provide a check for sStr length and SIZE or use dynamic allocs.
//Are you fat Albert from Hitch? kiddin'..
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
April 19th, 2007, 06:54 PM
#8
Re: How to convert a string into an array of char?
I tried using memcpy and by using viggy's method but each time i get an error saying that c_str() is not declared
-
April 19th, 2007, 09:31 PM
#9
Re: How to convert a string into an array of char?
 Originally Posted by Tsubasa
I tried using memcpy and by using viggy's method but each time i get an error saying that c_str() is not declared
Post your code, please.
Jeff
-
April 20th, 2007, 12:39 PM
#10
Re: How to convert a string into an array of char?
convert a string to char array???
a std::string IS a char array!
well not explicitly by stl....
and what is a "string"? is it char x[] = "ASDF" or char* x = "ASDF"; or std::string x = "SDFG" or CString x = "ASDF"; or wchar_t or TCHAR isntead of char.. or something else??
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|