-2

I'm trying to convert an std::string into a c string (my real problem is closer to this; I'm trying a workaround).

  1. How does one store an std::string as a character array?
  2. Why do std::string.data() and std::string.c_str() return char [(<anonymous> + 1)] instead of the expected const char*? Or do I have these mixed up, and if so, how do I work with these types?

    #include <string>
    #include <iostream>
    
    int main()
    {
      std::string sstring = "I'm an std string!";
    
      char cstring[sstring.size()];
    
      cstring = sstring.c_str();
      std::cout << std::string(sstring.c_str());
    
      return 0;
    }
    

Compilation results in

scratch.cpp:10:11: error: incompatible types in assignment of ‘const char*’ to ‘char [(<anonymous> + 1)]’

I wasn't able to find a relevant, preexisting question.

Community
  • 1
  • 1
jogama
  • 11
  • 1
  • 2
  • 3
    `std::string::data()` and `std::sring::c_str()` return `const char*`. – juanchopanza Apr 03 '17 at 19:10
  • 1
    Why do you think `.c_str()` returns `char [( + 1)]`? – melpomene Apr 03 '17 at 19:10
  • 1
    Why are you assigning a pointer to an array? – ForceBru Apr 03 '17 at 19:10
  • 2
    This is a blatant [XY problem](http://xyproblem.info/) scenario. Please state your real problem. – kennytm Apr 03 '17 at 19:11
  • 1
    The correct way to copy cstrings between char arrays is [`std::strcpy`](http://en.cppreference.com/w/cpp/string/byte/strcpy). The statement `cstring = sstring.c_str();` is trying to assign directly to an array but c arrays are not assignable. Note that `char cstring[sstring.size()];` is not standard c++, but some compiler support it as an extension. It's not portable. – François Andrieux Apr 03 '17 at 19:13
  • 1
    They do return `const char*`; `char [( + 1)]` is the type of your (non-standard) array. (`x = y` assigns `y` to `x`, not `x` to `y`.) – molbdnilo Apr 03 '17 at 19:14
  • Just use `const auto cstring = sstring.c_str();` . Btw; the deduced type of `cstring` will be `const char*`. – Jesper Juhl Apr 03 '17 at 19:15
  • @kennytm this is indeed an XY problem scenario. However, I very much feel I should know how to solve problem Y, and would like to learn to do so regardless. – jogama Apr 03 '17 at 19:16
  • 1
    The thing is, we don't know what problem Y actually is because we don't know the circumstance in which you encountered it because you didn't actually encounter it. It's not clear what the code is trying to do. Why isn't just calling `c_str()` and using the result sufficient? – David Schwartz Apr 03 '17 at 19:21
  • @melpomene I may be wrong. I though that ```c_str() returns ```char [( +1)]``` because this is what g++ complained about. – jogama Apr 03 '17 at 19:23
  • 1
    @jogama It returns `const char *`, more or less. – David Schwartz Apr 03 '17 at 19:26
  • @DavidSchwartz pardon; this is my first question on SO. The code I posted is self-contained; it's just that snippet compiled with ```g++ -std=c++11 scratch.cpp```. I'm not sure how I can further clarify Y; it's entirely the code in the question. – jogama Apr 03 '17 at 19:29
  • There's no question in the question. If the question is "why do I get that error message", the answer is that you can't use an array as an lvalue in C or C++. If you want to fill an array with values, you have to copy them in. So it's attempting to do something nonsensical. How you fix it depends on why you attempted to do something nonsensical, which we don't know. – David Schwartz Apr 03 '17 at 19:31
  • @jogama No, g++ is complaining that you're trying to assign to `char [( + 1)]`, which is the type of the `cstring` array in your code. – melpomene Apr 03 '17 at 19:47

3 Answers3

3

Just use a const char *

  std::string sstring = "I'm an std string!";

  const char * cstring = sstring.c_str();

  std::cout << cstring << std::endl;
Rama
  • 3,222
  • 2
  • 11
  • 26
1

As mentioned in the comments, this sound like an XY problem. But if you simply want to copy the string you can do it like so:

char cstring[sstring.size() + 1];
strcpy(cstring, sstring.c_str());

Just be aware that VLAs are not a part of C++ but g++ will 'go with it', see answers here.

Community
  • 1
  • 1
Jonas
  • 6,915
  • 8
  • 35
  • 53
-3

A working way to assign your string to a char array is the following:

#include <iostream>
#include <string>

int main(int argc, char **argv) {
  std::string s = "HELLO WORLD!";
  char *cs = (char *)s.c_str();

  std::cout << cs << '\n' << std::string(cs) << '\n';

  return 0;
}

OUTPUT:

CODE OUTPUT

This works because it casts the const char * to char * and therefore allows the assignment to go through, and although this may not be the best way it is very simple.

Andria
  • 4,712
  • 2
  • 22
  • 38