我在将我的 psswrd[] 的值重置为 NULL 时遇到问题,当我从另一个函数返回但不是使用相同的函数时它工作正常,例如我的第一个输入是“averylonginput”,因为输入是假的,我'我要再次输入正确的“thepass”,但现在的值是“thepassnginput”,其中前一个和现在的输入混合在一起,因为新的输入已经覆盖了前一个......
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <process.h>
success(){
clrscr();
printf("Press Any Key to Go Back and Login Again");
getch();
}
int cnt;
void login(){
char ch,
mypass[]="thepass",
psswrd[256]={0};
START:
clrscr();
printf("Enter Password: ");
clreol();
do{
ch=getch();
if( isprint(ch) ){
psswrd[ cnt++ ] = ch;
printf("%c", '#');
}
else
if(ch==8 && cnt){
psswrd[ cnt-- ] = '\0';
printf("%s", "\b \b");
}
}
while(ch!=13);
printf("\nCurrent Password: %s",psswrd); // Check Output
// Just an exit option
printf("\n\nPress Any Key to Continue, ESC to exit");
int opt;
opt=getch();
if(opt==27) exit(0);
else
//-------------------
if(!strcmp(psswrd,mypass)){
cnt=NULL;
clrscr();
printf("Success");
}
else{
psswrd[NULL];
cnt=NULL;
goto START;
}
}
void main(){
LOGIN: login();
success();
goto LOGIN;
}
由于代码已使用修复memset(psswrd, 0, sizeof(psswrd));
,当我来自另一个函数时,它在没有 memset 的情况下工作有什么区别?