1

Is there anything bad about the following code? Although it runs fine, but should I have allocated a memory to the character pointer first before initializing it?

const char *a;
string  b;
getline(cin, b) ;
a=&b[0u];
Moerwald
  • 10,448
  • 9
  • 43
  • 83
Vishal Sharma
  • 1,670
  • 20
  • 55
  • Try `a = &b.c_str()[0]` (or simply `a = b.c_str()` in the case of index `0`). – goodvibration Nov 06 '17 at 06:11
  • Just be careful, if the string is destroyed and your pointer to its buffer is not, then your pointer will dangle. – George Nov 06 '17 at 06:25
  • @George: `operator[]` returns a reference and no, it is a guarantee, so using `&b[0]` or `b.c_str()` or `b.data()` is largely a matter of taste, at least since C++11. – Christian Hackl Nov 06 '17 at 06:27
  • 1
    I would *avoid* doing this. Whenever you need to use a `const char*` simple use `b.c_str()`. – Galik Nov 06 '17 at 06:33
  • Possible duplicate of [How to convert a std::string to const char\* or char\*?](https://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char) – Ulrich Eckhardt Nov 06 '17 at 08:59

4 Answers4

3

It is fine since C++11.

Before C++11, there was no formal guarantee that operator[] would return a reference to a character that would be part of a null-terminated character array (i.e. a C-style string). One consequence of that missing guarantee was that &b[0u] would have been undefined behaviour if b was an empty non-const string.

(Actual implementations typically behaved correctly anyway, because that's the only sane way of implementing std::string, but that's another story.)

See also http://en.cppreference.com/w/cpp/string/basic_string/operator_at:

reference       operator[]( size_type pos );

(...)

If pos == size(), a reference to the character with value CharT() (the null character) is returned.

(since C++11)

Still, the code you've posted is not particularly good style. Why create a pointer with an uninitialised value and then assign it a value later on, and why bother with the more complicated syntax?

Here's an improved version of the code:

std::string b;
std::getline(std::cin, b);
auto const a = b.c_str();

In this version, a is const, so you cannot accidentally make it point to something else; you also make the compiler deduce the type (char const*) automatically. And c_str() is a clearer way of saying what your code actually means.

Daniel Kamil Kozar
  • 18,476
  • 5
  • 50
  • 64
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
1

As suggested by @goodvibration you should use c_str -> Reason: If you're using a compiler that doesn't support C++11 and higher versions of the standard, the std::string object won't include an appropriate null termination, which signals the end of the string when working with c-like methods. c_str takes care about the correct "format". See this stackoverflow link for additional information.

Hope that helps.

Moerwald
  • 10,448
  • 9
  • 43
  • 83
0

Simply use the string::c_str() method to get the const char *

string strText = "My String";
const char * chText = strText.c_str ();
0

There are two methods for this

  1. std::string::c_str
  2. std::string::data

You can use anyone based on taste, readability, etc.

std::string s;
std::getline(std::cin, s);
const char * a = s.c_str();
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50