4

Let's say I have a string called garbage.

Whatever's in garbage, I want to make a char array out of it. Each element would be one char of the string.

So, code would be similar to:

const int arrSize = sizeof(garbage); //garbage is a string
char arr[arrSize] = {garbage};

But, this will give an error "cannot convert string to char in initialization".

What is the correct way to do this? I just want to feed the darn thing a string and make an array out of it.

3
  • 4
    Assuming by string, you mean std::string, sizeof(garbage) probably doesn't do what you think it does. It just gives you the size of a std::string object, which is a compile time constant that has no relation to the length of the string, which is a runtime value. Commented Dec 4, 2012 at 1:14
  • 1
    You are correct, Ben. sizeof(garbage) does not give me what I want. Good catch! garbage.size() is the correct method. Commented Dec 4, 2012 at 1:20
  • 4
    Yes, but then arrSize cannot be used to specify the size of arr, because it would no longer be a compile time constant. Commented Dec 4, 2012 at 1:22

4 Answers 4

14

C++ std::string maintains an internal char array. You can access it with the c_str() member function.

#include <string>
std::string myStr = "Strings! Strings everywhere!";
const char* myCharArr = myStr.c_str();

Keep in mind that you cannot modify the internal array. If you want to do so, make a copy of the array and modify the copy.

Sign up to request clarification or add additional context in comments.

4 Comments

why a std::string would need to be converted to a char* is beyond me, but a good answer to this question.
Most common reason is for passing it to a function that only accepts C strings. As long as the function doesn't need to change the string, you can just pass it your std::string's internal buffer directly.
Exactly what I wanted. And to answer the question above, I'm implementing a string randomizer. I figure I can randomize to get a number in the range of the string size, and then pull a char from the string. Might be good for password generation if I have the entire alphanumeric set as the string.
You could do what you want without getting the array - myStr.size() will return the string's length, and you can access any character in the string at index i with myStr[i].
9

I think what you're after is a lot simpler:

char arr[] = "some interesting data (garbage)";

1 Comment

Thank you, This answered the reason I came to this question. I am trying to convert a piece of C code to C++. In the C code, it declares a 'char * foo = "string literal" ', and foo is expected to be a char *, not a const char *, in a later function.
0

You could just use garbage.data(), which is already a pointer to the first element of an array of chars containing your string data.

2 Comments

I don't think the initialization would work, since arr is not const.
@LuchianGrigore: No, of course, but I wouldn't even initialize anything. Just use the data directly. If you need a copy, copy the string.
-1

Or here is a heavy way to do so.

#include <string>
std::string temp = "something";
char* myChar = new char(temp.length());
for(int i = 0; i < temp.length(); ++i){
    a[i] = temp[i];
}

2 Comments

Kind reminder: remember to delete "a" after finished using it.
and by "a[i]" he means "myChar[i]". But char arr[]="something"; is more concise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.