【问题标题】:How to create a array of structs for matrixes如何为矩阵创建结构数组
【发布时间】:2020-03-03 21:17:37
【问题描述】:

基本上我有矩阵并想使用结构存储它们,但我需要使用动态内存分配来完成它

typedef struct {
    int mymatrix[5][5];  // the matrix 
    int column;          // will hold column number
    int row;             // this one is for row
} mystruct;

那么我怎样才能把它变成动态内存样式呢?

typedef struct {
    int **mymatrix;   // will have the matrix 
    int column;       // will hold column number
    int row;          // this one is for row
} mystruct;

是这样的吗?如果是这样,我在哪里给出 mymatrix 的大小?我也想将它作为一个结构数组,但我想在创建新对象时扩展该数组。如果我的问题不清楚,任何想法都会有所帮助,谢谢。

【问题讨论】:

  • Yout int **mymatrix 将是指向整数数组的指针(行)数组。所以首先分配指向行的指针数组,然后分配每一行的整数。
  • 可以使用mallocrealloc 分配和重新分配您的结构数组。
  • 有了这些提示,您应该能够解决您的问题。我认为这是家庭作业,所以我们不应该给出完整的解决方案。
  • 您可能希望将数组的维度存储在mystruct 的某个位置。这是rowcolumn 的用途,还是其他用途?
  • @IanAbbott 他们只是要显示矩阵有多少行和列

标签: c struct malloc realloc calloc


【解决方案1】:

这是一个演示程序,展示了如何为您的结构分配内存。

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

typedef struct {
    int **mymatrix;      // will have the matrix 
    size_t row;          // this one is for row
    size_t column;       // will hold column number
} mystruct;

int init( mystruct *s, size_t rows, size_t cols )
{
    s->mymatrix = malloc( rows * sizeof( int * ) );

    int success = s->mymatrix != NULL;

    if ( success )
    {
        size_t i = 0;

        while ( i < rows && ( s->mymatrix[i] = calloc( cols, sizeof( int ) ) ) != NULL ) 
        {
            i++;
        }           

        success = i == rows;

        if ( success )
        {
            s->row    = rows;
            s->column = cols;
        }
        else
        {
            for ( size_t j = 0; j < i; j++ )
            {
                free( s->mymatrix[j] );
            }

            free( s->mymatrix );

            s->mymatrix = NULL;
            s->row    = 0;
            s->column = 0;
        }
    }

    return success;
}

void fill( mystruct *s, int value )
{
    for ( size_t i = 0; i < s->row; i++ )
    {
        for ( size_t j = 0; j < s->column; j++ )
        {
            s->mymatrix[i][j] = value;
        }
    }
}

int main(void) 
{
    size_t n = 1;

    mystruct *s = malloc( n * sizeof( mystruct ) );

    if ( init( s, 5, 5 ) ) fill( s, 5 );

    mystruct *tmp = realloc( s, ( n + 1 ) * sizeof( mystruct ) );

    if ( tmp != NULL ) 
    {
        s = tmp,
        init( s + n, 10, 10 );
        fill( s + n, 10 );
        ++n;
    }

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < s[i].row; j++ )
        {
            for ( size_t k = 0; k < s[i].column; k++ )
            {
                printf( "%d ", s[i].mymatrix[j][k] );
            }

            putchar( '\n' );
        }

        putchar( '\n' );
    }

    for ( size_t i = 0; i < n; i++ )
    {
        if ( s[i].mymatrix != NULL )
        {
            for ( size_t j = 0; j < s[i].row; j++ )
            {
                free( s[i].mymatrix[j] );
            }

            free( s[i].mymatrix );
        }
    }

    free( s );

    return 0;
}

程序输出是

5 5 5 5 5 
5 5 5 5 5 
5 5 5 5 5 
5 5 5 5 5 
5 5 5 5 5 

10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 10 

【讨论】:

    猜你喜欢
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    相关资源
    最近更新 更多