c - 在 C 中的同一函数中将字符值重置为 null

标签 c

我在将 psswrd[] 的值重置为 NULL 时遇到问题,当我从另一个函数返回但不是使用相同的函数时它工作正常,例如我的第一个输入是“averylonginput”,因为输入是false,我要再次输入正确的“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 的情况下工作有什么区别?

最佳答案

表达式 psswrd[NULL] 本身不做任何事情。这与编写 psswrd[0] 一样。如果您启用警告,您应该收到关于此的警告,也许是一个,因为 NULL 可能是一个指针,您将其用作索引。

如果你想清除它,然后使用例如memset :

memset(psswrd, 0, sizeof(psswrd));

关于c - 在 C 中的同一函数中将字符值重置为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19564445/

相关文章:

c - 我如何为用 C 实现的解释语言提供垃圾收集?

switch 语句中第一个 'case' 之前的代码

c - 如何将单个字符从一个 char** 复制到另一个 char**?

c - 转发代理检测FIN包

c - scanf和getchar一起读取字符串

c - C stdlib 函数中的抽象格式说明符(例如 printf、scanf)

c - 在内部的双指针上使用 malloc

c - C语言代码中间包含什么?

我可以在c中使用->吗?这给我带来了一个错误

c - 了解用于接收 UDP 消息和打开 TCP 套接字的 Select 循环