6

How to assign a string to a char* (char pointer) in C++?

char *pw = some string
double-beep
  • 5,031
  • 17
  • 33
  • 41
Anuya
  • 8,082
  • 49
  • 137
  • 222
  • 1
    char* pw = "some string"; Is this not you are expecting? – Jagannath Feb 10 '10 at 08:38
  • if i use like char* pw = "some string"; i am getting exception. "corruption of heap" – Anuya Feb 10 '10 at 08:41
  • 1
    This is one of those "take a step back" questions. You can't really "assign a string" to a `char *`, because although a `char*` parameter is sometimes referred to as a "string parameter", it isn't actually a string, it's a pointer (to a string). If you're getting heap corruption, then the problem isn't the assignment, the problem is your management of allocated memory. Most likely there's something vital about allocated memory you don't know. Even if this one line could be replaced with a code snippet that stops your program crashing, the important thing is to understand the whole picture. – Steve Jessop Feb 10 '10 at 13:42

5 Answers5

8

For constant initialization you can simply use

const char *pw = "mypassword";

if the string is stored in a variable, and you need to make a copy of the string then you can use strcpy() function

char *pw = new char(strlen(myvariable) + 1);
strcpy(pw, myvariable);
// use of pw
delete [] pw; // do not forget to free allocated memory
sergiom
  • 4,791
  • 3
  • 24
  • 32
4

If you just want to assign a string literal to pw, you can do it like char *pw = "Hello world";.

If you have a C++ std::string object, the value of which you want to assign to pw, you can do it like char *pw = some_string.c_str(). However, the value that pw points to will only be valid for the life time of some_string.

Hans W
  • 3,851
  • 1
  • 22
  • 21
  • It's only guranteed to be valid until you perform a mutating operation on `some_string` . – Bill Feb 10 '10 at 15:11
3

If you mean a std::string, you can get a pointer to a C-style string from it, by calling c_str. But the pointer needs to be const.

const char *pw = astr.c_str();

If pw points to a buffer you've previously allocated, you might instead want to copy the contents of a string into that buffer:

astr.copy(pw, lengthOfBuffer);

If you're starting with a string literal, it's already a pointer:

const char *pw = "Hello, world".

Notice the const again - string literals should not be modified, as they are compiled into your program.

But you'll have a better time generally if you use std::string everywhere:

std::string astr("Hello, world");

By the way, you need to include the right header:

#include <string>
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
3

I think you may want to do this:

using namespace std;
string someString;
geline(cin,someString);
char *pw = strdup(someString.c_str());

But consider doing it another way. Check out http://tiswww.case.edu/php/chet/readline/rltop.html (GNU Readline library). I don't know details about it, just heard about it. Others may have more detailed or other tips for reading passwords from standard input.

If you only want to use it for a single call for something you do not need to copy the contents of someString, you may use someString.c_str() directly if it is required as const char *.

You have to use free on pw some time later,

Notinlist
  • 16,144
  • 10
  • 57
  • 99
  • 2
    `strdup` is handy but beware of portability issues: http://stackoverflow.com/questions/482375/c-strdup-function – Manuel Feb 10 '10 at 08:44
1

String must be enclosed in double quotes like :

char *pStr = "stackoverflow";

It will store this string literal in the read only memory of the program. And later on modification to it may cause UB.

Ashish
  • 8,441
  • 12
  • 55
  • 92