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

C语言字符数组报错求助:新手实现可修改3×3数组遇问题

Fixing Your 3×3 Character Array Setup

Hey there! I see you're getting started with arrays and functions in C—great first step! Let's walk through the issues in your code and get that 3×3 table of underscores working properly.

First, let's recap your goal: create a 3×3 char array filled with '_', then have functions to reset it and display it. Here's the code you shared, with the incomplete part noted:

#include<stdio.h>
#define SIZE 3
void clear_table(char ary[SIZE][SIZE]) {
    for(int i = 0; i < SIZE; i++) {
        for( int j = 0; j < SIZE; j++) {
            ary[i][j] = "_"; // Problem here!
        }
    }
}
void display_table(char ary[SIZE][SIZE]) {
    for( int i = 0; i < SIZE; i++) {
        for( int j = 0; j < SIZE; j++) {
            // Incomplete logic here
        }
    }
}

Key Issues to Fix

  • Wrong character assignment: In C, "_" is a string literal (a pointer to a const char array), but ary[i][j] is a single char variable. You need to use single quotes for a single character: '_'.
  • Incomplete display function: The display_table function doesn't actually print anything yet. We'll add code to print each element, plus some formatting to make the table look clean.
  • Missing main function: To run this code, you need a main() function that initializes the array, calls your functions, and demonstrates their use.

Corrected Full Code

#include<stdio.h>
#define SIZE 3

void clear_table(char ary[SIZE][SIZE]) {
    for(int i = 0; i < SIZE; i++) {
        for( int j = 0; j < SIZE; j++) {
            ary[i][j] = '_'; // Fixed: single quotes for char literal
        }
    }
}

void display_table(char ary[SIZE][SIZE]) {
    for( int i = 0; i < SIZE; i++) {
        for( int j = 0; j < SIZE; j++) {
            // Print each element with spacing
            printf(" %c ", ary[i][j]);
            // Add vertical separators between columns (except last)
            if (j < SIZE - 1) {
                printf("|");
            }
        }
        // Add horizontal separators between rows (except last)
        if (i < SIZE - 1) {
            printf("\n---+---+---\n");
        } else {
            printf("\n");
        }
    }
}

int main() {
    char table[SIZE][SIZE];
    
    // Initialize the table with underscores
    clear_table(table);
    
    // Display the empty table
    printf("Empty 3x3 Table:\n");
    display_table(table);
    
    // Example: Modify a cell to show how it works
    table[1][1] = 'X';
    printf("\nTable after modifying center cell:\n");
    display_table(table);
    
    return 0;
}

What This Does

  • clear_table: Properly assigns the single character '_' to every cell in the 2D array.
  • display_table: Prints the array in a readable table format with separators, so you can easily see each cell's value.
  • main: Sets up the array, calls clear_table to initialize it, displays the empty table, then modifies a cell and displays it again to show how you can edit the array later.

When you run this code, you'll see a clean 3×3 grid of underscores, then the same grid with an 'X' in the center—perfect for building something like a Tic-Tac-Toe game later!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:10:40