以指针给字符串赋值,并利用以指针为形参的函数输出字符串

本段代码利用指针给字符数组进行了赋值,并定义了一个具有指针形参的函数,输出了字符数组(遇到空字符会换行)

#include "stdafx.h"
#include "iostream"
using namespace std;

void ListString(char *szString[])
{
	char *pItem = *szString; 		//定义一个临时字符指针
	while (*pItem != '\0') 			//遍历字符串中的每一个字符
	{
		if (*pItem == ' ') 		//如果是空格则跳过
			cout << endl;
		else cout << *pItem;
		
		pItem++;				//指向字符串下一个字符
	}
	cout << endl;
}

int main()
{
	int i = 0;
	char *str = new char[5];
	for (i = 0; i < 4; i++)
	{
		if (i % 2 == 0)
		{
			*str = 'a';
		}
		else
		{
			*str = ' ';
		}
		/*cout << *str;*/
		str++;
	}
	str++;
	*str = '\0';
	str -= 5;
	ListString(&str);
	system("pause");
	return 0;
}

其输出结果为:

以指针给字符串赋值,并利用以指针为形参的函数输出字符串_第1张图片

你可能感兴趣的:(以指针给字符串赋值,并利用以指针为形参的函数输出字符串)