编程语言
首页 > 编程语言> > C++函数返回C风格的字符串

C++函数返回C风格的字符串

作者:互联网

函数返回C风格的字符串:
函数无法返回一个字符串,可以返回一个字符串的地址,这样做的效率更高。

#include <iostream>
char* buildstr(char c,int n);
int main()
{
	using namespace std;
	int times;
	char ch;

	cout << "Enter a character: ";
	cin >> ch;
	cout << "Enter an integer: ";
	cin >> times;
	char* ps = buildstr(ch,times);
	cout << ps << "DONE-" << ps << endl;
	return 0;
}
char* buildstr(char c, int n)
{
	char* pstr = new char[n+1];
	pstr[n] = '\0';
	while (n-->0)
	{
		pstr[n] = c;
	}
	return pstr;
}


标签:pstr,ch,函数,int,C++,times,char,字符串,cout
来源: https://blog.csdn.net/weixin_42105843/article/details/118385776