关于c++语言密码不回显的改进

我发现上次的超市购物管理系统的密码不回显可能导致数组下表越界以及返回局部变量的风险性问题 于是进行了改进

如下

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
typedef struct te{
	char a1[20];
}tep;
tep pass()
{
	char a[20],ch;
	int i=0;
	while((ch=getch())!='
')
	{
		if(i<20)
		{
			if(ch!='')
			{
				a[i++]=ch;
				putchar('*');
			}
			else if(i>0)
			{
				i--;
				putchar('');
				putchar(' ');
				putchar('');
			}
		}
	}
	a[i]='';
	tep aa;
	strcpy(aa.a1,a);
	return aa;
}
int main()
{
	tep aaa=pass();
	printf("%s",aaa.a1);
	getch();
}

  通过结构体把数组进行打包会更加安全一些或者是这样更加保险

原文地址:https://www.cnblogs.com/miaos/p/miaoz-1-6.html