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

C语言字符串变量name1在代码执行后丢失内容求助

问题分析与修复方案

你的代码中name1内容丢失的核心原因是缓冲区溢出破坏了栈内存,同时还有几处语法和潜在风险问题,具体如下:

核心问题:check数组缓冲区溢出

你声明了char check[3];来接收用户输入的"yes"或"no",但C语言中字符串需要以\0(空字符)结尾:

  • "yes"包含3个字符,加上终止符需要4个字节的空间,check[3]只能容纳3个字节,写入时会越界覆盖相邻内存(刚好可能是name1所在的栈空间),导致name1的字符串被损坏。

其他问题

  1. 语法错误:变量声明行char name1[30], name2[30], phonenumber[30], final中,final未指定数据类型且未被使用,会导致编译失败。
  2. strcat潜在溢出风险:直接使用strcat拼接name1、空格和name2,如果用户输入的名字总长度超过name1的30字节上限,会触发缓冲区溢出,破坏其他变量。

修复后的代码

// Online C compiler to run C program online
#include <stdio.h>
#include <string.h>

int main() {
    // 修正:移除无用的final变量,修复语法错误
    char name1[30], name2[30], phonenumber[30];
    int userdetailchecker = 2;
    int finalconfirmation = 0;

    printf("\n\n******GMI KOPERASI ORDER AND SELF-PICK-UP SERVICE******\n");
    printf("|You have to register your details to access this service|\n");

    while(userdetailchecker != 1){
        printf("Please enter your first name: ");
        scanf("%s", name1);
        printf("Please enter your last Name: ");
        scanf("%s", name2);

        // 修正:用snprintf替代strcat,避免缓冲区溢出
        snprintf(name1, sizeof(name1), "%s %s", name1, name2);

        printf("Is your name %s?\n1.Yes\n2.No\n", name1);
        scanf("%d", &userdetailchecker);
    }

    printf("Great! Your orders would be associated with the username %s\n", name1);
    printf("Please enter your phone number: ");
    scanf("%s", phonenumber);

    while(strlen(phonenumber) < 9 || strlen(phonenumber) > 13){
        printf("Invalid phone number(Must be 9 to 13 digits only)\n");
        printf("Please enter your phone number: ");
        scanf("%s", phonenumber);
    }

    printf("You have successfully registered your phone number! (%s)\n", phonenumber);
    printf("\nHello user %s!\n", name1);
    printf("Continue to end section? y/n?\n");

    // 修正:扩大check数组容量,至少能容纳"yes"+终止符(4字节)
    char check[10];
    scanf("%s", check);

    if(strcmp(check, "yes") == 0)
    {
        printf("\nPlease confirm your user details\n");
        printf("Username = %s\n", name1);
        printf("Phone number = %s\n", phonenumber);
        printf("1.Yes\n2.No\n");

        while(1){
            scanf("%d", &finalconfirmation);
            if(finalconfirmation == 2)
            {
                printf("Please log out of this session.\n");
                break;
            }
            else if(finalconfirmation == 1){
                // 添加确认成功逻辑
                printf("Details confirmed successfully!\n");
                break;
            }
            else{
                printf("Invalid input, please enter 1 or 2.\n");
            }
        }
    }
    else{
        return 0;
    }
    return 0;
}

关键修改说明

  1. 移除了无用的final变量,修复编译语法错误。
  2. 将char check[3];改为char check[10];,留有足够空间存放用户输入的字符串和终止符,避免缓冲区溢出。
  3. 用snprintf替代strcat拼接名字:snprintf会自动限制写入长度(不超过name1的大小),从根源避免缓冲区溢出风险。
  4. 给最后的确认循环添加了对输入1的处理,避免无限循环。

内容的提问来源于stack exchange,提问作者LeCoding

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 08:43:15