当试图到达Typedef两个dinatial阵列时,堆叠粉碎了,ADRESS存储在struct中

发布于 2025-01-28 19:41:02 字数 1212 浏览 4 评论 0原文

我有这个两个谷物阵列的Typedef,并遵守他的地址

typedef double mat[MAT_SIZE][MAT_SIZE];

typedef struct matList {
    char *name;
    mat *matrix;
} matList;

和指针,指向Matlist的Arry,

mat MAT_A, MAT_B, MAT_C, MAT_D, MAT_E, MAT_F;
matList* mats[MAT_COUNT];
int i;

for (i = 0; i < MAT_COUNT; i++) {
   mats[i] = NULL;
}

mats[0] = create_mat("MAT_A", &MAT_A);
mats[1] = create_mat("MAT_B", &MAT_B);
mats[2] = create_mat("MAT_C", &MAT_C);
mats[3] = create_mat("MAT_D", &MAT_D);
mats[4] = create_mat("MAT_E", &MAT_E);
mats[5] = create_mat("MAT_F", &MAT_F);

我想拥有一个能够到达垫子的功能 我把这件事加入了

void restart_mat(matList *mats[]) {
    int i, j, k;

    if (mats == NULL) {
        return;
    }

    for (k = 0; k < MAT_COUNT; k++) {
        if (mats[k] != NULL) {
            for (i = 0; i < MAT_SIZE; i++) {
                for (j = 0; j < MAT_SIZE; j++) {
                    *(mats[k]->matrix)[i][j] = 0;
                }
            }
        }
    }
}

我的要求,但后来我得到了 -

*** stack smashing detected ***: ./mainmat terminated
Aborted (core dumped)

i have this typedef of two dinantial array, and strust the keep his adress

typedef double mat[MAT_SIZE][MAT_SIZE];

typedef struct matList {
    char *name;
    mat *matrix;
} matList;

and pointer to arry of matList

mat MAT_A, MAT_B, MAT_C, MAT_D, MAT_E, MAT_F;
matList* mats[MAT_COUNT];
int i;

for (i = 0; i < MAT_COUNT; i++) {
   mats[i] = NULL;
}

mats[0] = create_mat("MAT_A", &MAT_A);
mats[1] = create_mat("MAT_B", &MAT_B);
mats[2] = create_mat("MAT_C", &MAT_C);
mats[3] = create_mat("MAT_D", &MAT_D);
mats[4] = create_mat("MAT_E", &MAT_E);
mats[5] = create_mat("MAT_F", &MAT_F);

i want to have a function that ill be able to get to the mat and put valuse inside the two dimantial array
i wirte this one

void restart_mat(matList *mats[]) {
    int i, j, k;

    if (mats == NULL) {
        return;
    }

    for (k = 0; k < MAT_COUNT; k++) {
        if (mats[k] != NULL) {
            for (i = 0; i < MAT_SIZE; i++) {
                for (j = 0; j < MAT_SIZE; j++) {
                    *(mats[k]->matrix)[i][j] = 0;
                }
            }
        }
    }
}

it dose what i ask to but then i get -

*** stack smashing detected ***: ./mainmat terminated
Aborted (core dumped)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红衣飘飘貌似仙 2025-02-04 19:41:02

数据成员矩阵具有指针类型double( *)[mat_size] [mat_size]

typedef struct matList{
char *name;
mat *matrix;
}matList;

因此,指针矩阵必须指向类型double [mat_size]的有效二维数组,[mat_size]并删除指针,您将获得该数组的lvalue。因此,循环看起来像是

for(k=0; k<MAT_COUNT;k++){

    if(mats[k] !=NULL){

        for(i=0; i<MAT_SIZE; i++){
            for(j=0; j<MAT_SIZE; j++){

                           ( *mats[k]->matrix)[i][j]=0;
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
            }
          }
 }
}

一样编写的语句

( *mats[k]->matrix)[i][j]=0;

您也可以

mats[k]->matrix[0][i][j]=0;

像为此语句

*(mats[k]->matrix)[i][j]=0;

,然后等效于此,

*(mats[k]->matrix[i][j] ) = 0;

因为后缀下标运算符[]具有比Dereference Operation *<<<<<<更高/代码>。

请注意函数create_mat必须将指针返回到类型Matlist的动态分配对象。

The data member matrix has the pointer type double ( * )[MAT_SIZE][MAT_SIZE].

typedef struct matList{
char *name;
mat *matrix;
}matList;

So the pointer matrix must point to a valid two-dimensional array of the type double [MAT_SIZE][MAT_SIZE] and dereferencing the pointer you will get lvalue of the array. Thus the loops will look like

for(k=0; k<MAT_COUNT;k++){

    if(mats[k] !=NULL){

        for(i=0; i<MAT_SIZE; i++){
            for(j=0; j<MAT_SIZE; j++){

                           ( *mats[k]->matrix)[i][j]=0;
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
            }
          }
 }
}

Instead of the statement

( *mats[k]->matrix)[i][j]=0;

you may also write

mats[k]->matrix[0][i][j]=0;

As for this statement

*(mats[k]->matrix)[i][j]=0;

then it is equivalent to

*(mats[k]->matrix[i][j] ) = 0;

because the postfix subscript operator [] has a higher precedence than the dereference operator *.

Pay attention to that the function create_mat must return a pointer to a dynamically allocated object of the type matList.

一曲爱恨情仇 2025-02-04 19:41:02

2D数组MAT_A,MAT_B,MAT_C,MAT_D,MAT_E,MAT_F被定义为本地对象,在函数中具有自动存储,可以调用create_mat。在分配的Matlist结构中设置了指向这些数组的指针。函数返回后,必须不再引用这些数组。

除非有令人信服的理由分配2D数组,而Matlist则分别将矩阵定义为结构成员而不是指针:

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

typedef double mat[MAT_SIZE][MAT_SIZE];

typedef struct matList {
    const char *name;
    mat matrix;
} matList;

void init_mat(mat m) {
    for (int i = 0; i < MAT_SIZE; i++) {
        for (int j = 0; j < MAT_SIZE; j++) {
            m[i][j] = 0;
        }
    }
}

matList *create_mat(const char *name) {
    matList *tempMat = malloc(sizeof(*tempMat));
    if (tempMat != NULL) {
        tempMat->name = name;
        init_mat(tempMat->matrix);
    }
    return tempMat;
}

// return non zero if successful
int allocate_matList(matList *mats) {
    for (int i = 0; i < MAT_COUNT; i++) {
        mats[i] = NULL;
    }
    mats[0] = create_mat("MAT_A");
    mats[1] = create_mat("MAT_B");
    mats[2] = create_mat("MAT_C");
    mats[3] = create_mat("MAT_D");
    mats[4] = create_mat("MAT_E");
    mats[5] = create_mat("MAT_F");
    return mats[0] && mats[1] && mats[2] &&
           mats[3] && mats[4] && mats[5];
}

void restart_matList(matList *mats) {
    if (mats != NULL) {
        for (int k = 0; k < MAT_COUNT; k++) {
            if (mats[k] != NULL)
                init_mat(mats[k]->matrix);
        }
    }
}

The 2D arrays MAT_A, MAT_B, MAT_C, MAT_D, MAT_E, MAT_F are defined as local objects with automatic storage in the function that calls create_mat. A pointer to these arrays is set in the allocated matList structure. These arrays must no longer be referenced after the function returns.

Unless there is a compelling reason to allocate the 2D arrays and the matList structures separately, you should define the matrix as a struct member instead of a pointer:

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

typedef double mat[MAT_SIZE][MAT_SIZE];

typedef struct matList {
    const char *name;
    mat matrix;
} matList;

void init_mat(mat m) {
    for (int i = 0; i < MAT_SIZE; i++) {
        for (int j = 0; j < MAT_SIZE; j++) {
            m[i][j] = 0;
        }
    }
}

matList *create_mat(const char *name) {
    matList *tempMat = malloc(sizeof(*tempMat));
    if (tempMat != NULL) {
        tempMat->name = name;
        init_mat(tempMat->matrix);
    }
    return tempMat;
}

// return non zero if successful
int allocate_matList(matList *mats) {
    for (int i = 0; i < MAT_COUNT; i++) {
        mats[i] = NULL;
    }
    mats[0] = create_mat("MAT_A");
    mats[1] = create_mat("MAT_B");
    mats[2] = create_mat("MAT_C");
    mats[3] = create_mat("MAT_D");
    mats[4] = create_mat("MAT_E");
    mats[5] = create_mat("MAT_F");
    return mats[0] && mats[1] && mats[2] &&
           mats[3] && mats[4] && mats[5];
}

void restart_matList(matList *mats) {
    if (mats != NULL) {
        for (int k = 0; k < MAT_COUNT; k++) {
            if (mats[k] != NULL)
                init_mat(mats[k]->matrix);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文