Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to copy char * into a string and vice-versa

Tags:

c++

If i pass a char * into a function. I want to then take that char * convert it to a std::string and once I get my result convert it back to char * from a std::string to show the result.

  1. I don't know how to do this for conversion ( I am not talking const char * but just char *)
  2. I am not sure how to manipulate the value of the pointer I send in.

so steps i need to do

  1. take in a char *
  2. convert it into a string.
  3. take the result of that string and put it back in the form of a char *
  4. return the result such that the value should be available outside the function and not get destroyed.

If possible can i see how it could be done via reference vs a pointer (whose address I pass in by value however I can still modify the value that pointer is pointing to. so even though the copy of the pointer address in the function gets destroyed i still see the changed value outside.

thanks!

like image 541
user295030 Avatar asked Apr 01 '10 22:04

user295030


People also ask

How do I copy char pointer to string?

Syntax: char* strcpy (char* destination, const char* source); The strcpy() function is used to copy strings. It copies string pointed to by source into the destination . This function accepts two arguments of type pointer to char or array of characters and returns a pointer to the first string i.e destination .

How do I copy a Std string?

Just call either the data() or c_str() member functions of the std::string class, to get the char* pointer of the string object. The strcpy() function doesn't have overload to accept two std::string objects as parameters.

How do I copy a character from one string to another in C++?

strcpy() is a standard library function in C/C++ and is used to copy one string to another. In C it is present in string. h header file and in C++ it is present in cstring header file.

How do I capture a character from a string in C++?

string at() in C++std::string::at can be used to extract characters by characters from a given string. Syntax 2: const char& string::at (size_type idx) const idx : index number Both forms return the character that has the index idx (the first character has index 0).


2 Answers

Converting a char* to a std::string:

char* c = "Hello, world";
std::string s(c);

Converting a std::string to a char*:

std::string s = "Hello, world";
char* c = new char[s.length() + 1];
strcpy(c, s.c_str());

// and then later on, when you are done with the `char*`:
delete[] c;

I prefer to use a std::vector<char> instead of an actual char*; then you don't have to manage your own memory:

std::string s = "Hello, world";
std::vector<char> v(s.begin(), s.end());
v.push_back('\0'); // Make sure we are null-terminated
char* c = &v[0];
like image 76
James McNellis Avatar answered Oct 11 '22 17:10

James McNellis


You need to watch how you handle the memory from the pointer you return, for example the code below will not work because the memory allocated in the std::string will be released when fn() exits.

const char* fn(const char*psz) {
    std::string s(psz);
    // do something with s
    return s.c_str();   //BAD
}

One solution is to allocate the memory in the function and make sure the caller of the function releases it:

const char* fn(const char*psz) {
    std::string s(psz);
    // do something with s
    char *ret = new char[s.size()]; //memory allocated
    strcpy(ret, s.c_str());
    return ret;
}
....
const char* p = fn("some text");
//do something with p
delete[] p;// release the array of chars

Alternatively, if you know an upper bound on the size of the string you can create it on the stack yourself and pass in a pointer, e.g.

void fn(const char*in size_t bufsize, char* out) { 
    std::string s(psz);
    // do something with s
    strcpy_s(out, bufsize, s.c_str()); //strcpy_s is a microsoft specific safe str copy
}
....
const int BUFSIZE = 100;
char  str[BUFSIZE];

fn("some text", BUFSIZE, str);
//ok to use str (memory gets deleted when it goes out of scope)
like image 1
hamishmcn Avatar answered Oct 11 '22 18:10

hamishmcn