You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言注册系统中变量x从3变为0?数组遭意外覆盖

C语言登录注册系统越界问题修复方案

问题根源分析

  • 数组越界篡改内存:defaultuser和defaultpass初始化仅定义了3个元素,合法索引范围是0、1、2。你用x=3访问defaultuser[3]属于数组越界操作,会破坏栈上其他变量(比如x)的内存空间,这就是x突然变为0的核心原因。
  • 变量冲突引发异常:函数开头定义的int i = 0在密码输入循环中被持续自增,后续登录逻辑的for循环又重新定义了int i,外层i的异常值会导致passinput数组越界,进一步破坏内存。
  • scanf参数错误:登录时scanf(" %s", &userinput);中,userinput本身就是数组地址,加&会导致类型不匹配,引发未定义行为。

修复后的代码

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

void loginsystem(){
    // 扩大数组容量,预留足够的用户存储空间
    char defaultuser[10][100] = {"12291995", "12221999", "12241344"};
    char defaultpass[10][100] = {"test1", "test2", "test3"};
    // 用user_count记录当前用户数量,替代硬编码的索引值
    int user_count = 3;
    int indicator = 1;
    char userinput[20], passinput[20], choicest[20]; 
    char* choicecmp;

    system("cls");
    printf("\nSystem");
    printf("\nWould you like to create an account or log into an existing account? (Type Signup or Login) ");
    // 限制输入长度,避免choicest数组越界
    scanf(" %19[^\n]", choicest);
    printf("当前用户数:%d\n", user_count);

    choicecmp = strlwr(choicest);
    if(strcmp(choicecmp, "signup") == 0 || strcmp(choicecmp, "sign up") == 0 )
    {
        // 检查用户数量是否达到上限
        if(user_count >= 10){
            printf("\n用户数量已达上限!");
            getch();
            return;
        }
        printf("\nEnter your ID Number: ");
        // 用user_count作为合法索引添加新用户
        scanf(" %99s", defaultuser[user_count]);
        printf("\nEnter your password: ");
        scanf(" %99s", defaultpass[user_count]);
   
        getch();
        user_count++; 
        printf("新用户数:%d\n", user_count);
        getch();
        // 自动跳转至登录逻辑
        choicecmp = "login";
    }
    if(strcmp(choicecmp, "login") == 0 || strcmp(choicecmp, "log in") == 0 ){
        do{
            system("cls");
            printf("\nPlease login!");
            printf("\n\nUsername (ID Number): ");
            // 修正scanf参数,去掉多余的&
            scanf(" %19s", userinput);
            printf("\nPassword: ");
            // 密码输入循环使用局部变量i,避免与外层变量冲突
            int i = 0;
            do{
                passinput[i] = getch();
                if(passinput[i] != '\r')
                    printf("*");
                i++;
                // 限制密码长度,防止passinput数组越界
                if(i >= 19) break;
            }while(passinput[i-1] != '\r');
            passinput[i-1] = '\0';

            int login_success = 0;
            for(int i = 0; i < user_count; i++){ 
                if(strcmp(userinput, defaultuser[i]) == 0 && strcmp(passinput, defaultpass[i]) == 0)
                {
                    system("cls");
                    login_success = 1;
                    printf("\nWelcome back User");
                    break;
                }
            }
            if(login_success){
                getch();
                break;
            } else {
                printf("\n用户名或密码错误,请重试!");
                getch();
            }
        }while(indicator);
    }
}

核心修复点

  1. 扩大数组容量并新增user_count变量记录用户数量,彻底避免硬编码索引导致的越界。
  2. 密码输入循环使用局部变量i,消除变量冲突问题。
  3. 修正scanf参数错误,同时限制输入长度防止数组越界。
  4. 添加用户数量上限检查,避免后续再次触发越界操作。

内容的提问来源于stack exchange,提问作者Reaper Rapper-san's Eye

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 16:34:52