简体   繁体   English

用C中的结构指针进行数组分配

[英]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). 我无法更改它们,因为它们是由称为flint的库(快速数论的库)提供的。

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. 所以我的问题是持有一组nmod_poly_t对象。 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? 那么,如何做我需要声明我的动态数组S存储里面我strucs?

Thanks in advance 提前致谢

Following will work, assuming memory allocated properly for key.s and initialized 假设为key.s正确分配了key.s并进行了初始化, key.s工作将起作用

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

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

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