简体   繁体   English

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

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

I am trying to use SuperLU for a matrix inversion but I am unable to access the final result. 我试图使用SuperLU进行矩阵求逆,但我无法访问最终结果。 It uses a few structures for the inversion and I know the answer is inside a structure but I can't reference it. 它使用一些结构进行反演,我知道答案是在结构内,但我不能引用它。

B is defines as a supermatrix which has the format: 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;

Based on the Stype the structure of store changes. 基于Stype的商店结构变化。 For B the struct used for *Store is: 对于B,用于* Store的结构是:

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

As a result the final structure of B should be: 因此,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 ];
    }
}

Now I want to copy the values out from nzval but I am not sure how to. 现在我想从nzval复制值,但我不知道如何。

I tried to do B.Store.nzval but the error is "request for member `nzval' in something not a structure or union" 我试图做B.Store.nzval,但错误是“请求成员`nzval'在一些不是结构或联合的东西”

Also

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

and a few other things like this but not sure how to work this through. 还有一些像这样的东西,但不知道如何解决这个问题。

Thanks a lot! 非常感谢!

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

If you want to be terse, you can put it all together: 如果你想要简洁,你可以把它们放在一起:

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

This is because Store is a pointer in the struct. 这是因为Store是结构中的指针。 And also DNFormat is declared as a void *; 并且DNFormat也被声明为void *; this means that Store is a void pointer which cannot be dereferenced without a cast; 这意味着Store是一个void指针,如果没有强制转换,它就无法解除引用; and also the fact that it is a pointer means that you must use the dereference operator -> 而且它是一个指针意味着你必须使用解除引用运算符->

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

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

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