Perhaps I don’t understand your question.
A char* is just a pointer; as every pointer, you need a (owned) memory area to initialize it to.
If you want to inizialise it to a string literal, since string literals are stored in read-only memory, you need to declare it const.
#include <cstring>
#include <iostream>
int main()
{
char* TAG = "";
std::cout << std::strlen(TAG) << '\n';
}
Output:
g++ -std=c++2a main.cpp -o main.exe
main.cpp: In function 'int main()':
main.cpp:7:17: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
7 | char* TAG = "";
| ^~
- - -
@JUANDENT, my code was just an example: let me point out that, since ‘c’ is a local variable, if you returned ‘buffer’ from a function, it would point to a non-owned memory area.
Here’s a different example:
dunno about the OP but I have to deal with a number of 'C interfaced' tools that send and expect back a C-string. If the code is doing anything interesting or can make use of string/algorithm tools, I will convert it to and from and use what I needed. But if all it is doing is something really simple, I may not bother.
An empty C-string doesn't mean there are no characters in the string, there has to be the terminating '\0' character.
Creating a C-string like this -- a pointer to a char -- is rather messy, requiring a lot of (IMO) unnecessary memory management during its lifetime. std::string does all the memory management for you.
That's the most convoluted way I've ever seen to declare a character ;)
I have a feeling we're not seeing the full picture of what's happening.
Also, because I don't think anyone else said it: char* buffer = ""s.c_str();
Even if this were valid, you would be getting a pointer to a temporary object. ""s is no longer an object after that statement finishes.
char * b = &s[0]; //s is string. this works, but its not useful. s may not have a zero where you want it for c-string, because string works off the length not a terminal. if you modify b's data, and change the length, you will have bad problems. this may be what you wanted to express, but don't do this.
you really only need 3 things in practice for most anything you probably want to do here.
char array, and use the array name as a pointer as needed (to pass to functions etc)
char *, to accept the result of strstr or as a location inside your char array for any magic you are working, and
a string object if you want to convert to and from (inefficient if careless). Those items with string algorithms and C-string functions can do 100% of anything normal without any fuss.
no dynamic memory should be needed for a few small strings. Nothing weird or complicated. I know you guys are having fun, but if he needs to actually use a c-string for something ... there is no reason to over-engineer it.
it does not!
it is a temporary string variable in that specific statement, not the c_str() call that is a problem.
it creates a temp string, gets the pointer, and then destroys the temp object, leaving you hanging.
if you said this:
string s = "blah";
char* b = s.c_str(); // may need to watch out for const here
cout << s; //fine, s is untouched.
cout << b; //fine, b is still valid.
do you see it? there is no 'variable' of type string in the questioned example: look at it, what is the string variable's name?
one more time, its like doing this:
char *b;
{
string s = "word";
b = s.c_str();
} //s is destroyed, its scope-block ends here
b is invalid here... same as the first example, but explicitly showing the hidden variable.