Jump to content

how to convert from char * to string?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
alirezan

alirezan

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Hi
I am trying to find a way to convert from char * to string.
I have:

char * buf = "test";

and I want to look for a substring in this. I tried using strcpy:

string s;
strcpy (s, buf);

but it keeps telling me: "No instance of function "strcpy" matches the argument list.

Anybody knows a way of doing this?

Thanks

#2
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
IDK about that error, but first I must tell you, strcpy is NOT compatible with C++ strings. If you want to convert from char * to string, you would do something like this:
char *but = "test";

string s;

s = buf;
If you want to use strcpy, however, for copying one C string to another, you must make sure you have the library included:
#include <cstring> //For C++

#include <string.h> //For C

BTW, use the code tags for you code. In the editor, it is the # symbol.
Latinamne loqueris?

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
You can simply use the (overloaded) assignment operator to assign a char * to a std::string. This will copy all the data over automatically.

If you're trying to copy from a std::string to a char *, the best way to do that is with strncpy(), but you have to get the char data from the string first!

  size_t my_str_size_with_null_end = my_str->size() + 1;
  char *buffer = malloc(my_str_size_with_null_end);
  strncpy(buffer, my_str->c_str(), my_str_size_with_null_end);

Wow I changed my sig!

#4
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

mebob said:

...
char *but = "test";

string s;

s = buf;
...
In 'char * but= "test";' , I suppose you meant 'char * buf= "test";' .


mebob said:

...In the editor, it is the # symbol.
This editor? I thought it was [CODE] and [/CODE] that make code tags.

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
@RhetoricalRuvim: Both of them work. I prefer [noparse][code][/noparse] tags myself, along with any other tags I can use. The only real trouble I have with the quick reply window here is that it keeps the formatting of pasted text, which forces me to put it through a text editor first. -_-
Wow I changed my sig!

#6
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
LOL typo, sorry.
Latinamne loqueris?

#7
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
No one really mentioned the std::string ctor:
char* buf = "test";
std::string s(buf);

A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users