当试图到达Typedef两个dinatial阵列时,堆叠粉碎了,ADRESS存储在struct中
我有这个两个谷物阵列的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据成员
矩阵
具有指针类型double( *)[mat_size] [mat_size]
。因此,指针矩阵必须指向类型
double [mat_size]的有效二维数组,[mat_size]
并删除指针,您将获得该数组的lvalue。因此,循环看起来像是一样编写的语句
您也可以
像为此语句
,然后等效于此,
因为后缀下标运算符
[]
具有比Dereference Operation*<<<<<<
更高/代码>。请注意函数
create_mat
必须将指针返回到类型Matlist
的动态分配对象。The data member
matrix
has the pointer typedouble ( * )[MAT_SIZE][MAT_SIZE]
.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 likeInstead of the statement
you may also write
As for this statement
then it is equivalent to
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 typematList
.2D数组
MAT_A,MAT_B,MAT_C,MAT_D,MAT_E,MAT_F
被定义为本地对象,在函数中具有自动存储,可以调用create_mat
。在分配的Matlist
结构中设置了指向这些数组的指针。函数返回后,必须不再引用这些数组。除非有令人信服的理由分配2D数组,而
Matlist
则分别将矩阵定义为结构成员而不是指针: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 callscreate_mat
. A pointer to these arrays is set in the allocatedmatList
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: