#include
#include
using namespace std;
int main()
{
char ch1[] = "woainidan";
char ch2[] = "iloveyou";
char result[strlen(ch1) + strlen(ch2)];
strcpy(result, ch1);
strcat(result, ch2);
cout << ch1 << " " << ch2<<" " << result << endl;
return 0;
}
char result[strlen(ch1) + strlen(ch2)];
->
char * result = new char[strlen(ch1) + strlen(ch2)];
char result[sizeof(ch1) + sizeof(ch2)];
#include "stdafx.h"
#include <string>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
char ch1[] = "woainidan";
char ch2[] = "iloveyou";
char* result = new char[strlen(ch1) + strlen(ch2)];
strcpy_s(result, strlen(ch1), ch1);
strcat_s(result, strlen(ch2), ch2);
std::cout << ch1 << " " << ch2<<" " << result << std::endl;
return 0;
}
char ch1[] = "woainidan";
char ch2[] = "iloveyou";
size_t isize = strlen(ch1) + strlen(ch2) + 1;
char* result = new char[isize];
memset(result, 0, strlen(ch1) + strlen(ch2) + 1);
strcpy_s(result, strlen(ch1) + 1, ch1);
strcat_s(result + strlen(ch1), isize - strlen(ch1), ch2);
std::cout << ch1 << " " << ch2<<" " << result << std::endl;
同意一楼,但解决方法不是唯一的,一楼也许改变了原意。
char result[sizeof(ch1) + sizeof(ch2)];
因为sizeof不是函数,是个运算符,在预编译时即转为了常量