我是靠谱客的博主 跳跃丝袜,这篇文章主要介绍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] = '';
	while (n-->0)
	{
		pstr[n] = c;
	}
	return pstr;
}


最后

以上就是跳跃丝袜最近收集整理的关于C++函数返回C风格的字符串的全部内容,更多相关C++函数返回C风格内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(134)

评论列表共有 0 条评论

立即
投稿
返回
顶部