三维数组指针定义和初始化例子

在Linux C环境下,定义一个三维的结构体数组指针并遍历它涉及几个步骤。

首先,需要定义一个结构体,然后定义一个指向这个结构体数组的指针,最后使用嵌套循环来遍历它。

咱们定义了一个名为Person的结构体,它包含nameage两个成员。然后定义了一个指向三维Person数组的指针,并遍历它。

示例:

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
  
// 定义Person结构体  
typedef struct {  
    char name[50];  
    int age;  
} Person;  
  
int main() {  
    // 定义三维数组的大小  
    const int depth = 2;  
    const int numRows = 3;  
    const int numCols = 4;  
  
    // 动态分配三维数组的内存  
    Person ***people = (Person ***)malloc(depth * sizeof(Person **));  
    if (people == NULL) {  
        perror("Failed to allocate memory for people array");  
        return EXIT_FAILURE;  
    }  
  
    for (int i = 0; i < depth; i++) {  
        people[i] = (Person **)malloc(numRows * sizeof(Person *));  
        if (people[i] == NULL) {  
            perror("Failed to allocate memory for a layer of people");  
            // 释放已经分配的内存  
            for (int j = 0; j < i; j++) {  
                free(people[j]);  
            }  
            free(people);  
            return EXIT_FAILURE;  
        }  
  
        for (int j = 0; j < numRows; j++) {  
            people[i][j] = (Person *)malloc(numCols * sizeof(Person));  
            if (people[i][j] == NULL) {  
                perror("Failed to allocate memory for a row of people");  
                // 释放已经分配的内存  
                for (int k = 0; k < j; k++) {  
                    free(people[i][k]);  
                }  
                free(people[i]);  
                // 释放之前已经分配的内存  
                for (int l = 0; l < i; l++) {  
                    free(people[l]);  
                }  
                free(people);  
                return EXIT_FAILURE;  
            }  
        }  
    }  
  
    // 初始化数组  
    for (int i = 0; i < depth; i++) {  
        for (int j = 0; j < numRows; j++) {  
            for (int k = 0; k < numCols; k++) {  
                snprintf(people[i][j][k].name, sizeof(people[i][j][k].name), "Person %d-%d-%d", i, j, k);  
                people[i][j][k].age = i * j * k + 10;  
            }  
        }  
    }  
  
    // 遍历三维数组  
    for (int i = 0; i < depth; i++) {  
        for (int j = 0; j < numRows; j++) {  
            for (int k = 0; k < numCols; k++) {  
                printf("Name: %s, Age: %d\n", people[i][j][k].name, people[i][j][k].age);  
            }  
        }  
    }  
  
    // 释放内存  
    for (int i = 0; i < depth; i++) {  
        for (int j = 0; j < numRows; j++) {  
            free(people[i][j]);  
        }  
        free(people[i]);  
    }  
    free(people);  
  
    return EXIT_SUCCESS;  
}

这个示例中:

  • 我们首先定义了三维数组的大小:depth(深度或层数)、numRows(行数)和numCols(列数)。
  • 然后,我们为三维数组的每一层分配了指向二维数组的指针数组的内存。
  • 对于每一层,我们又为每一行分配了指向一维数组的指针的内存。
  • 对于每一行,我们最后分配了存储Person结构体的内存。
  • 在初始化时,我们使用三层嵌套的循环来设置每个Person结构体的nameage字段。
  • 遍历数组时,我们使用三层嵌套的循环来访问并打印每个Person结构体的信息。
  • 最后,我们按照相反的顺序释放了之前分配的所有内存,首先释放每一行的内存,然后释放每一层的内存,最后释放整个三维数组的指针。

学废了么,<(* ̄▽ ̄*)/

相关推荐

  1. 三维数组指针定义初始化例子

    2024-03-13 22:56:06       23 阅读
  2. C++ 各种数据结构定义以及初始化

    2024-03-13 22:56:06       15 阅读
  3. C# 各种数据结构定义以及初始化

    2024-03-13 22:56:06       17 阅读
  4. c 一,二,三维数组的定义赋值

    2024-03-13 22:56:06       42 阅读
  5. 指针数组与函数例题3

    2024-03-13 22:56:06       28 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-13 22:56:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-13 22:56:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-13 22:56:06       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-13 22:56:06       20 阅读

热门阅读

  1. C#写入和调用方法

    2024-03-13 22:56:06       23 阅读
  2. c语言之函数声明

    2024-03-13 22:56:06       25 阅读
  3. 题目 3150: 冶炼金属

    2024-03-13 22:56:06       23 阅读
  4. SQL 中 IN 与 <= 且 >= 的效率比较

    2024-03-13 22:56:06       26 阅读
  5. 职业规划随笔

    2024-03-13 22:56:06       20 阅读
  6. k8s之配置springboot项目

    2024-03-13 22:56:06       19 阅读
  7. go优雅重试

    2024-03-13 22:56:06       14 阅读