簡體   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