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];
5
  • Try a = &b.c_str()[0] (or simply a = b.c_str() in the case of index 0). Commented Nov 6, 2017 at 6:11
  • Just be careful, if the string is destroyed and your pointer to its buffer is not, then your pointer will dangle. Commented Nov 6, 2017 at 6: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. Commented Nov 6, 2017 at 6:27
  • 1
    I would avoid doing this. Whenever you need to use a const char* simple use b.c_str(). Commented Nov 6, 2017 at 6:33
  • Possible duplicate of How to convert a std::string to const char* or char*? Commented Nov 6, 2017 at 8:59

4 Answers 4

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.

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

Comments

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.

3 Comments

You should read all answers to your own link! stackoverflow.com/a/11752722/3313064 explains that your first sentence is incorrect since C++11.
so std::string in C++11 and above is terminated by null character?
0

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

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

Comments

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();

Comments

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.