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


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;
}

其输出结果为:

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

相关文章:

  • 2021-06-14
  • 2021-12-29
  • 2022-12-23
  • 2022-01-03
  • 2021-08-08
  • 2022-12-23
  • 2021-10-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案