const char* c_str ( ) const;
Get C string equivalent
Generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.
A terminating null character is automatically appended.

const char* data() const;
Get string data
Returns a pointer to an array of characters with the same content as the string.
Notice that no terminating null character is appended (see member c_str for such a functionality).

  

c_str()字符串后有'\0',而data()没有

 

简单的const string 转void*

#include <iostream>
using namespace std;

int main()
{
	const string c = "90908080";
	const char* buf = c.c_str();
	char* tmp = const_cast<char*> (buf);
	cout<<buf<<endl;
	cout<<tmp<<endl;
	void* v=reinterpret_cast<void*>(tmp);
	cout<<v<<endl;
	cout<<(char*)(v)<<endl;
   return 0;
}

  

相关文章:

  • 2021-05-16
  • 2022-01-28
  • 2021-07-07
  • 2021-11-26
猜你喜欢
  • 2021-10-13
  • 2021-12-05
  • 2021-07-15
  • 2021-10-18
  • 2021-10-15
  • 2021-06-04
相关资源
相似解决方案