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

C语言新手求助:如何动态创建可按[i][j]访问的二维字符串数组

动态创建可通过things[i][j]访问的二维字符串数组

先明确两种常见需求,对应不同实现方式:


需求1:things[i][j]表示第i行的第j个字符串

也就是二维数组的每个元素是完整字符串,比如存储多行多列的名称。

实现步骤:

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

int main() {
    int total_rows = 7; // 对应你代码里的total
    int total_cols = 5; // 每行的字符串数量

    // 第一步:分配行指针数组,每个元素指向一行的字符串数组
    char*** things = malloc(sizeof(char**) * total_rows);
    if (things == NULL) { // 检查内存分配是否成功
        perror("malloc failed");
        return 1;
    }

    // 第二步:给每行分配列的空间
    for (int i = 0; i < total_rows; i++) {
        things[i] = malloc(sizeof(char*) * total_cols);
        if (things[i] == NULL) {
            perror("malloc failed");
            // 分配失败时释放已分配的内存
            for (int k = 0; k < i; k++) free(things[k]);
            free(things);
            return 1;
        }
    }

    // 第三步:给每个元素赋值字符串
    for (int i = 0; i < total_rows; i++) {
        for (int j = 0; j < total_cols; j++) {
            // 方式1:指向常量字符串(不可修改)
            things[i][j] = "kitty";
            // 方式2:动态分配可修改的字符串(需释放内存)
            // things[i][j] = strdup("kitty");
            // 或手动分配+复制:
            // things[i][j] = malloc(strlen("kitty") + 1);
            // strcpy(things[i][j], "kitty");
        }
    }

    // 访问示例:打印所有字符串
    for (int i = 0; i < total_rows; i++) {
        for (int j = 0; j < total_cols; j++) {
            printf("%s ", things[i][j]);
        }
        printf("\n");
    }

    // 第四步:释放内存(按分配顺序反向释放)
    for (int i = 0; i < total_rows; i++) {
        for (int j = 0; j < total_cols; j++) {
            // 若用了strdup或malloc分配字符串,需执行此行
            // free(things[i][j]);
        }
        free(things[i]);
    }
    free(things);

    return 0;
}

需求2:things[i][j]表示第i行第j个字符

也就是二维字符数组,每行是一个字符串,比如存储多行文本。

实现步骤:

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

int main() {
    int total_rows = 7;
    int max_char_per_row = 10; // 每行最多存储的字符数(含结束符'\0')

    // 第一步:分配行指针数组
    char** things = malloc(sizeof(char*) * total_rows);
    if (things == NULL) {
        perror("malloc failed");
        return 1;
    }

    // 第二步:给每行分配字符空间
    for (int i = 0; i < total_rows; i++) {
        things[i] = malloc(sizeof(char) * (max_char_per_row + 1)); // +1给'\0'留位置
        if (things[i] == NULL) {
            perror("malloc failed");
            for (int k = 0; k < i; k++) free(things[k]);
            free(things);
            return 1;
        }
    }

    // 第三步:赋值字符串
    for (int i = 0; i < total_rows; i++) {
        strcpy(things[i], "kitty"); // 复制字符串到行数组
        // 也可以直接输入:scanf("%s", things[i]);
    }

    // 访问示例:打印单个字符或整行
    printf("第0行第2个字符:%c\n", things[0][2]);
    for (int i = 0; i < total_rows; i++) {
        printf("第%d行:%s\n", i, things[i]);
    }

    // 第四步:释放内存
    for (int i = 0; i < total_rows; i++) {
        free(things[i]);
    }
    free(things);

    return 0;
}

注意事项:

  • 内存分配后必须检查malloc返回值是否为NULL,避免空指针错误。
  • 释放内存要按分配的反向顺序,先释放内层,再释放外层,防止内存泄漏。
  • 常量字符串(如"kitty")不可修改;若需修改内容,必须用strdup或malloc+strcpy动态分配内存。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 23:41:32