Questions tagged [c-str]

Anything related to `c_str` method of class `std::basic_string` of C++ standard library.

Anything related to c_str method of class std::basic_string of C++ standard library.

See documentation of c_str on CPPreference.com.

73 questions
142
votes
6 answers

string c_str() vs. data()

I have read several places that the difference between c_str() and data() (in STL and other implementations) is that c_str() is always null terminated while data() is not. As far as I have seen in actual implementations, they either do the same or…
leon
118
votes
7 answers

What is the lifetime of the result of std::string::c_str()?

In one of my programs, I have to interface with some legacy code that works with const char*. Lets say I have a structure which looks like: struct Foo { const char* server; const char* name; }; My higher-level application only deals with…
ereOn
  • 53,676
  • 39
  • 161
  • 238
95
votes
7 answers

What is the use of the c_str() function?

I understand c_str converts a string, that may or may not be null-terminated, to a null-terminated string. Is this true? Can you give some examples?
Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199
38
votes
4 answers

c_str() vs. data() when it comes to return type

After C++11, I thought of c_str() and data() equivalently. C++17 introduces an overload for the latter, that returns a non-constant pointer (reference, which I am not sure if it's updated completely w.r.t. C++17): const CharT* data() const; (1) …
gsamaras
  • 71,951
  • 46
  • 188
  • 305
26
votes
3 answers

Difference in c_str function specification between C++03 and C++11

In the C++ reference of c_str() in std::string the following appears: Return value Pointer to the underlying character storage. data()[i] == operator[](i) for every i in [0, size()) (until C++11) data() + i == &operator[](i) for every i in [0,…
Chiel
  • 6,006
  • 2
  • 32
  • 57
16
votes
3 answers

What's the difference between std::string::c_str and std::string::data?

Why would I ever want to call std::string::data() over std::string::c_str()? Surely there is some method to the standard's madness here...
fbrereto
  • 35,429
  • 19
  • 126
  • 178
10
votes
1 answer

How can I create and pass a null-terminated array of C-strings (char**) in rust?

I'm playing around with a new init system with #![no_std] and extern crate rlibc and making syscalls with asm, and currently trying to not allocate memory either. So the scope of possible tools I have is limited. I need to call the execve syscall,…
Ant Manelope
  • 531
  • 1
  • 6
  • 15
8
votes
4 answers

Returning 'c_str' from a function

This is from a small library that I found online: const char* GetHandStateBrief(const PostFlopState* state) { static std::ostringstream out; // ... rest of the function ... return out.str().c_str() } In my code I am doing this: const…
user199421
  • 1,850
  • 2
  • 18
  • 18
7
votes
3 answers

std::string::c_str & Null termination

I've read various descriptions of std::string::c_str including questions raised on SO over the years/decades, I like this description for its clarity: Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a…
tuk
  • 367
  • 1
  • 4
  • 10
4
votes
5 answers

Setting a char array with c_str()?

char el[3] = myvector[1].c_str(); myvector[i] is a string with three letters in. Why does this error?
pighead10
  • 4,155
  • 10
  • 34
  • 52
4
votes
2 answers

C++ Change working Directory from User Input

I'm designing a mock shell program, and I can't quite mimic the "cd" command. I've tried chdir(), but that wasn't working, so I moved on to trying to change the environmental variable "PWD=" Here's what I have, and I think this may be close.…
twsmale
  • 139
  • 1
  • 5
  • 14
3
votes
4 answers

what is use of C_str() function in C/C++

Can anybody tell me what is the use of c_str() function in C/C++?. In which case it is necessary to use it?.
sachin
  • 311
  • 2
  • 13
  • 24
3
votes
6 answers

Store c_str() as char *

I'm trying to use the function with the following declaration: extern int stem(struct stemmer * z, char * b, int k)1 I'm trying to pass a C++ string to it, so I thought I'd use the c_str() function. It returns const char *. When I try to pass it to…
muttley91
  • 12,278
  • 33
  • 106
  • 160
3
votes
5 answers

.c_str() weirdness? Data changes without rhyme or reason?

I have this simple function: const wchar_t *StringManager::GetWCharTStar(int stringId) { std::wstring originalString = StringManager::GetString(stringId); const wchar_t *retStr = originalString.c_str(); return retStr; } At the second…
user189320
2
votes
1 answer

Is it safe to use std::string::c_str() to modify the underlying std::string?

I came across some code which has several instances of the bellow example: std::string str = "This is my string"; char* ptr = const_cast(str.c_str()); ptr[5] = 'w'; ptr[6] = 'a'; In this simplified example there is an assignment of…
anastaciu
  • 23,467
  • 7
  • 28
  • 53
1
2 3 4 5