繁体   English   中英

如何访问另一个结构中作为指针的结构内的元素?

[英]How to access an element inside a struct that is inside another struct as a pointer?

我试图使用SuperLU进行矩阵求逆,但我无法访问最终结果。 它使用一些结构进行反演,我知道答案是在结构内,但我不能引用它。

B被定义为具有以下格式的超级矩阵:

typedef struct {
Stype_t Stype; /* Storage type: indicates the storage format of *Store. */
Dtype_t Dtype; /* Data type. */
Mtype_t Mtype; /* Mathematical type */
int nrow; /* number of rows */
int ncol; /* number of columns */
void *Store; /* pointer to the actual storage of the matrix */
} SuperMatrix;

基于Stype的商店结构变化。 对于B,用于* Store的结构是:

typedef struct {
int lda; /* leading dimension */
void *nzval; /* array of size lda-by-ncol to represent
a dense matrix */
} DNformat;

因此,B的最终结构应该是:

B = { Stype = SLU_NC; Dtype = SLU_D; Mtype = SLU_GE; nrow = 5; ncol = 5;
*Store = { lda = 12;
     nzval = [ 19.00, 12.00, 12.00, 21.00, 12.00, 12.00, 21.00,
     16.00, 21.00, 5.00, 21.00, 18.00 ];
    }
}

现在我想从nzval复制值,但我不知道如何。

我试图做B.Store.nzval,但错误是“请求成员`nzval'在一些不是结构或联合的东西”

DNformat **g = B.Store;
int *r = *(g->nzval);

还有一些像这样的东西,但不知道如何解决这个问题。

非常感谢!

DNformat *g = (DNformat *)B.store;
int *r = (int *)g->nzval;

如果你想要简洁,你可以把它们放在一起:

int *r = (int *)((DNformat *)B.store)->nzval;

这是因为Store是结构中的指针。 并且DNFormat也被声明为void *; 这意味着Store是一个void指针,如果没有强制转换,它就无法解除引用; 而且它是一个指针意味着你必须使用解除引用运算符->

((DNFormat *)B.Store)->nzval

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM