简体   繁体   中英

Array assignment with struct pointers in C

I need to hold a dynamic array of structs.

The types are defined like this. I'm not able to change those, because they are given by a library called flint (library for fast number theory).

typedef struct
{
    mp_ptr coeffs;
    slong alloc;
    slong length;
    nmod_t mod;
} nmod_poly_struct;

typedef nmod_poly_struct nmod_poly_t[1];

and my struct is defined like this:

typedef struct {
    mp_limb_t D;
    mp_limb_t length;
    nmod_poly_t *s;
} she_key_symmetric_t;

So my problem is to hold a set of nmod_poly_t objects. I initialize them and want so store them in the array.

nmod_poly_t poly;
nmod_poly_init(poly);

she_key_symmetric_t key;
// init and stuff
key.s[0] = poly; // This line does not work, because 
// it always says "array type 'nmod_poly_t' (aka 
// 'nmod_poly_struct[1]') is not assignable"

In the next step I have to get the values back out of the array

she_key_symmetric_t key;
// fully initialized key
nmod_poly_t poly = key.s[0]; 

So how do I need to declare my dynamic array s to store my strucs inside it?

Thanks in advance

Following will work, assuming memory allocated properly for key.s and initialized

nmod_poly_t poly; 
poly[0] =key.s[0][0];

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