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

为何这段C语言代码无法将单词存入二维数组?

问题分析与修复

你的代码无法存储内容并输出空白,核心问题集中在以下几点:

1. 赋值方向完全颠倒

你当前是把空的目标数组(startswithA/B/C)内容赋值给holder,而非将用户输入的holder内容复制到目标数组。这直接导致目标数组始终为空,holder的输入内容也被清空。

2. scanf_s使用错误

holder[i]本身就是字符数组的首地址,不需要加&;且scanf_s必须指定缓冲区大小以防止溢出,正确写法需补充缓冲区长度参数。

3. 目标数组索引管理错误

你用循环变量i作为startswithA/B/C的索引,但需求是每种开头仅存2个单词,若输入顺序混乱会导致存储位置错乱,且多数位置会空置。需为每个目标数组单独设置计数器,控制存储位置。


修复后的代码

#include <stdio.h>
#include <string.h> // 用于strcpy简化数组复制

int main()
{
    char startswithA[2][10] = {}; // 每种仅需存2个,缩小数组规模
    char startswithB[2][10] = {};
    char startswithC[2][10] = {};
    char holder[10] = {}; // 单次仅存一个单词,改用一维数组即可
    int countA = 0, countB = 0, countC = 0;

    printf("Enter a word starting with a, b or c (write 2 of each)\n");
    for (int i = 0; i < 6; i++)
    {
        printf("Now entering word #%d\n", i + 1);
        // 正确使用scanf_s:指定缓冲区大小,无需&
        scanf_s("%s", holder, (unsigned)_countof(holder));
        
        if (holder[0] == 'a' || holder[0] == 'A') // 兼容大写输入
        {
            if (countA < 2) // 限制最多2个a开头单词
            {
                strcpy(startswithA[countA], holder); // 替代手动循环复制
                countA++;
            }
            else
            {
                printf("You've already entered 2 words starting with a!\n");
                i--; // 重新输入当前项
            }
        }
        else if (holder[0] == 'b' || holder[0] == 'B')
        {
            if (countB < 2)
            {
                strcpy(startswithB[countB], holder);
                countB++;
            }
            else
            {
                printf("You've already entered 2 words starting with b!\n");
                i--;
            }
        }
        else if (holder[0] == 'c' || holder[0] == 'C')
        {
            if (countC < 2)
            {
                strcpy(startswithC[countC], holder);
                countC++;
            }
            else
            {
                printf("You've already entered 2 words starting with c!\n");
                i--;
            }
        }
        else
        {
            printf("Invalid input: please enter a word starting with a, b or c.\n");
            i--; // 重新输入当前项
        }
    }

    // 按分类输出结果
    printf("\nWords starting with a:\n");
    for (int i = 0; i < countA; i++)
    {
        printf("%s\n", startswithA[i]);
    }

    printf("\nWords starting with b:\n");
    for (int i = 0; i < countB; i++)
    {
        printf("%s\n", startswithB[i]);
    }

    printf("\nWords starting with c:\n");
    for (int i = 0; i < countC; i++)
    {
        printf("%s\n", startswithC[i]);
    }

    return 0;
}

额外优化说明

  • 将holder改为一维数组,节省不必要的空间占用;
  • 用strcpy替代手动循环复制字符,代码更简洁且不易出错;
  • 添加输入数量限制检查,避免每种开头单词超过2个;
  • 兼容大写字母输入,提升用户体验;
  • 按分类输出结果,避免原代码中空行过多的混乱输出。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 08:15:34