简体   繁体   中英

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. 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:

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. For B the struct used for *Store is:

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 = { 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.

I tried to do B.Store.nzval but the error is "request for member `nzval' in something not a structure or union"

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. And also DNFormat is declared as a void *; this means that Store is a void pointer which cannot be dereferenced without a cast; and also the fact that it is a pointer means that you must use the dereference operator ->

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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