C语言指针二维数组表示矩阵

C语言中动态的内存管理的相关函数在头文件stdlib.h中,定义如下:

void* malloc( size_t size );
// 分配size字节的未初始化内存。
// 成功时返回指向新分配内存的指针。必须用free()释放内存。
// 失败时,返回空指针。

void* calloc( size_t num, size_t size );
// 为num个size字节大小的对象的数组分配内存,并将分配存储中的所有字节初始化为零。
// 成功时返回指向新分配内存的指针。必须用free()释放内存。
// 失败时,返回空指针。

void *realloc( void *ptr, size_t new_size );
// 重新分配内存区域。
// 若ptr非NULL,ptr就必须是malloc()、calloc()或realloc()所返回的指针。
// 并且仍未被free()或realloc()释放内存。否则,结果未定义。
// 操作:
// 1.扩张或收缩ptr所指向的现有内存区域。
//   新旧大小中的较小者范围内的区域的内容保持不变。
//   若扩张范围,则数组新增部分的内容是未定义的。
// 2.分配一个大小为new_size字节的新内存块,
//   复制大小等于新旧大小中较小者的内存区域,然后释放旧内存块。
// 若无足够内存,则不释放旧内存块,并返回空指针。
// 若ptr为NULL,则行为与调用malloc(new_size)相同。

void free( void* ptr );
// 释放指针ptr指向的分配内存。

​​​​​​​

/**
 * Copyright[2024] <Mopoke>
 */
#ifndef I_MATRIX
#define I_MATRIX

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

typedef struct i_matrix {
    int **cells;
    int rows;
    int columns;
} Matrix;

Matrix* createMatrix(const int row, const int column);
bool isValid(Matrix* matrix, const int row, const int column);
void setMatrix(Matrix* matrix, const int row, const int column, const int value);
int getCell(Matrix* matrix, const int row, const int column);
void freeMatrix(Matrix* matrix);

#endif
// The end of header file i_matrix.h

​​​​​​​

/**
 * Copyright[2024] <Mopoke>
 */
#include "./i_matrix.h"

Matrix* createMatrix(const int row, const int column) {
    Matrix *new = calloc(1, sizeof(Matrix));
    int index_row = 0;
    int index_column = 0;

    new->rows = row;
    new->columns = column;
    new->cells = calloc(row, sizeof(int*));

    if (new->cells == NULL) {
        fprintf(stderr, "Error - unable to allocate required memory for new->cells.\n");
    }

    while (index_row < row) {
        new->cells[index_row] = calloc(column, sizeof(int));

        if (new->cells[index_row] == NULL) {
            fprintf(stderr, "Error - unable to allocate required memory for new->cells[%d].\n", index_row);
        }

        index_row += 1;
    }

    return new;
}

bool isValid(Matrix* matrix, const int row, const int column) {
    if (row > matrix->rows || column > matrix->columns) {
        return false;
    }
    if (row < 1 || column < 1) {
        return false;
    }

    return true;
}

void setMatrix(Matrix* matrix, const int row, const int column, const int value) {
    matrix->cells[row - 1][column - 1] = value;
}

int getCell(Matrix* matrix, const int row, const int column) {
    return matrix->cells[row - 1][column - 1];
}

void freeMatrix(Matrix* matrix) {
    int index = 0;

    while (index < matrix->rows) {
        free(matrix->cells[index]);
        index += 1;
    }

    free(matrix->cells);
    free(matrix);
}
// The end of file i_matrix.c
/**
 * Copyright[2024] <Mopoke>
 */
#include <stdio.h>
#include <stdlib.h>

#include "./i_matrix.h"

#define ROW 3
#define COLUMN 5

int main(int argc, char* argv[]) {
    Matrix *m = createMatrix(ROW, COLUMN);
    int row = 0;
    int column = 0;

    if (isValid(m, 1, 1)) { setMatrix(m, 1, 1, 101); }
    if (isValid(m, 3, 5)) { setMatrix(m, 3, 5, 121); }

    printf("The number of rows is %d.\n", m->rows);
    printf("The number of columns is %d.\n", m->columns);
    for (row = 1; row <= ROW; row++) {
        for (column = 1; column <= COLUMN; column++) {
            printf("%d\t", getCell(m, row, column)); 
        }

        printf("\n");
    }

    freeMatrix(m);

    return EXIT_SUCCESS;
}
// The end of file main.c
The number of rows is 3.
The number of columns is 5.
101	0	0	0	0	
0	0	0	0	0	
0	0	0	0	121	

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值