char*,const char*和string的相互转换 + 三种版本字符串

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


void main(int argc, char* argv[])
{
	const char *p1 = "111";
	string p2(p1);
	printf("const char *  to  string        : %s
",p2.c_str());


	string p3("222");
	const char* p4 = p3.c_str();
	printf("string        to  const char *  : %s
",p4);


	char * p5 = "333";
	const char * p6 = p5;
	printf("char*         to  const char *  : %s
",p6);


	const char * p7 = "444";
	char * p8 = new char[100];//足够长
	strcpy(p8,p7);
	printf("const char *  to char*          : %s
",p8);


	char *p9 = "555";
	string p10(p9);
	printf("char *        to string         : %s
",p10.c_str());
	
	string p11("666");
	int len = p11.length();
	char *p12 = new char[len+1];
	strcpy(p12,p11.c_str());
	printf("string        to char *         : %s
",p12);
	
	return ;
}


三种版本 字符串:

#include <Windows.h>

int WINAPI wWinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPTSTR lpCmdLine,
	int nCmdShow)
{
	int i;
	INT i2;

	char c;
	CHAR c2;

	wchar_t c3;
	WCHAR c4;

	TCHAR c5;

	PCHAR a;
	PWCHAR a2;
	PTCHAR a3;

	PSTR b;
	LPSTR b2 = "string";
	PWSTR b3;
	LPWSTR b4 = L"string";
	PTSTR b5;
	LPTSTR b6 = TEXT("string");

	PCSTR d;
	PCWSTR d2;
	PCTSTR d3;

	MessageBoxW(NULL,L"这是W版的MessageBox",L"Test",MB_OK);
	MessageBoxA(NULL,"这是A版的MessageBox","Test",MB_OK);
	MessageBox(NULL,TEXT("这是T版的MessageBox"),TEXT("Test"),MB_OK);

	system("pause");
	return 0;
}



原文地址:https://www.cnblogs.com/zcc1414/p/3982446.html