在C结构的初始化变量矩阵 [英] Initializing an variable matrix in a structure C

查看:208
本文介绍了在C结构的初始化变量矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在C.结构初始化变量矩阵麻烦一直在读一对夫妇的职位(的帖子),但我似乎无法修复它。不要问我为什么但是对于一个任务,我需要初始化位于结构的矩阵。

我的code的结构是:

  typedef结构迷宫{
    INT行;
    INT列;
    焦炭矩阵[行] [列];
}迷宫;

通过调用不同的功能,读取某个文件后,我需要通过其给定参数初始化一个矩阵,所以2个参数行和列。

我的code的初始化函数是:

 结构的迷宫* init_maze(INT行,诠释列){    结构迷宫mazeFilled;
    结构迷宫* mazePointer;    mazeFilled.row =排;
    mazeFilled.column =列;    返回mazePointer;
}

但你能猜到这是行不通的,人们已经提到,这是很难从2个变量创建一个矩阵。谁能解释一下我做错了。

编辑1:

我按照然而,结构仍然是相同的职位改变了我的code。我需要分配指针,所以它即使是外舀保持活跃。

我的新code为init FUNCT是:

 结构的迷宫* init_maze(无效){    INT排= 6;
    INT列= 10;
    结构迷宫* mazePointer =的malloc(sizeof的(* mazePointer));    mazePointer->排排=;
    mazePointer->列=列;    返回mazePointer;
}

编辑2:

我想我discoverd什么我的错误是,我没有为矩阵分配内存。
我的新初始化函数:

 结构的迷宫* init_maze(无效){INT排= 6;
INT列= 10;迷宫* mazePointer;
mazePointer =的malloc(sizeof的(迷宫));
mazePointer->排排=;
mazePointer->列=列;
mazePointer->矩阵=的malloc(行*的sizeof(字符*));对于(INT I,I<排;我++){
    对于(INT焦耳; J<列; J ++){
    mazePointer - >矩阵[I] [J] =的malloc(1 * sizeof的(字符*));
    }
}返回mazePointer;}

我仍然不知道如何分配的内存只是第一阵列,​​[1]。谁能告诉我,如果我在正确的方向?


解决方案

  

谁能解释一下我做错了什么?


您正在返回一个指针到本地/自动存储时间结构是不能保证存在超出范围 {} 的功能。


有许多方法可以做到这一点。这样做的目的是,即使在函数返回后,返回它仍然活着,活跃的结构。


较常用的方式做,这是动态分配的结构,填充它,然后返回一个指向它。

 结构的迷宫* init_maze(INT行,诠释列)
{    结构迷宫* pmazeFilled =的malloc(sizeof的(* pmazeFilled));    pmazeFilled->排排=;
    pmazeFilled - >列=列;    返回pmazeFilled;
}

记住调用者必须通过调用免费上返回的指针释放分配的内存。

I am having trouble with initializing a variable matrix in a structure in C. Have been reading a couple of posts(post) but I cant seem to fix it. Don't ask me why but for an assignment I need to initialize a matrix located in a structure.

My code for the struct is:

typedef struct maze{
    int row;
    int column;
    char matrix[row][column];
}maze;

By calling a different function, after reading a certain file, I need to initialize a matrix by its given parameters, so 2 parameters "row" and "column".

My code for the init function is:

struct maze* init_maze(int row, int column) {

    struct maze mazeFilled;
    struct maze *mazePointer;

    mazeFilled.row = row;
    mazeFilled.column = column;

    return mazePointer;
}

But as you can guess this is not working, people already mentioned that it is hard to create a matrix from 2 variables. Could anyone explain what I am doing wrong.

EDIT 1:

I changed my code according to the posts the struct however remains the same. I needed to allocate the pointer so it even stays active outside the scoop.

My new code for the init funct is:

struct maze* init_maze(void) {

    int row = 6;
    int column = 10;
    struct maze *mazePointer = malloc(sizeof (*mazePointer));

    mazePointer->row = row;
    mazePointer->column = column;

    return mazePointer;
}   

EDIT 2:

Think I discoverd what my error was, I did not allocate memory for the matrix. My new init function:

struct maze* init_maze(void) {

int row = 6;
int column = 10;

maze *mazePointer;
mazePointer = malloc(sizeof(maze));
mazePointer->row = row;
mazePointer->column = column;
mazePointer->matrix =  malloc(row * sizeof(char*));

for (int i; i < row; i++) {
    for(int j; j < column; j++) {
    mazePointer -> matrix[i][j] = malloc(1 * sizeof(char*));
    }
}

return mazePointer;

}

I am still not sure how to allocate the memory for just the first array, [i]. Could anyone tell me if I am in the right direction?

解决方案

Could anyone explain what I am doing wrong?

You are returning a pointer to a local/automatic storage duration structure which is not guaranteed to exist beyond the scope { } of the function.


There are a number of ways to do this. The aim is return a structure which remains alive and active even after returning from the function.


The more commonly used way to do this is to allocate the structure dynamically, populate it and return a pointer to it.

struct maze* init_maze(int row, int column) 
{

    struct maze *pmazeFilled = malloc(sizeof(*pmazeFilled));

    pmazeFilled->row = row;
    pmazeFilled ->column = column;

    return pmazeFilled;
}

Remember the caller must free the allocated memory by calling free on the returned pointer.

这篇关于在C结构的初始化变量矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆