当我尝试使用结构数组的第一个元素的地址而不是指向结构的指针时,我收到编译器警告。以下编译没有警告:
typedef struct {
int val;
} my_struct;
typedef my_struct my_t[1];
typedef struct {
my_struct **t_ptr;
} my_other_struct;
int main(void)
{
my_struct t0;
my_t t1 = {t0};
t1->val = 0;
my_other_struct t2;
my_struct *t_array[] = {t1};
t2.t_ptr = t_array;
return 0;
}
但是当我将my_struct *
的两次出现更改为my_t
时,(
typedef struct {
int val;
} my_struct;
typedef my_struct my_t[1];
typedef struct {
my_t *t_ptr;
} my_other_struct;
int main(void)
{
my_struct t0;
my_t t1 = {t0};
t1->val = 0;
my_other_struct t2;
my_t t_array[] = {t1};
t2.t_ptr = t_array;
return 0;
}
)我收到警告
test.c: In function ‘main’:
test.c:20:5: warning: initialization makes integer from pointer without a cast [enabled by default]
my_t t_array[] = {t1};
^
test.c:20:5: warning: (near initialization for ‘t_array[0][0].val’) [enabled by default]
为什么我收到此警告并且我做错了什么?
修改
以下是我用作参考的一些代码:
typedef fmpz fmpz_t[1];
typedef struct
{
fmpz * coeffs;
slong alloc;
slong length;
} fmpz_poly_struct;
typedef fmpz_poly_struct fmpz_poly_t[1];
typedef struct
{
fmpz_poly_struct *num;
fmpz_poly_struct *den;
} fmpz_poly_q_struct;
typedef fmpz_poly_q_struct fmpz_poly_q_t[1];
void fmpz_poly_q_zero(fmpz_poly_q_t rop)
{
fmpz_poly_zero(rop->num);
fmpz_poly_set_si(rop->den, 1);
}
void fmpz_poly_q_one(fmpz_poly_q_t rop)
{
fmpz_poly_set_si(rop->num, 1);
fmpz_poly_set_si(rop->den, 1);
}
void fmpz_poly_q_neg(fmpz_poly_q_t rop, const fmpz_poly_q_t op)
{
fmpz_poly_neg(rop->num, op->num);
fmpz_poly_set(rop->den, op->den);
}
我现在确实看到他们在_struct *
结构中使用了_t
而不是fmpz_poly_q_t
。