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\n",p2.c_str());


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


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


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


	char *p9 = "555";
	string p10(p9);
	printf("char *        to string         : %s\n",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\n",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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值