正如我在主题中所写的那样,当我尝试将指针传递给使用结构体数组构造的结构体时,我遇到了冲突类型错误。您是否有删除此错误的建议?我想念的是什么?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define N 10
void count_length(struct abc *_el);
struct vector {
double x;
double y;
};
struct abc {
struct vector vec;
double length;
};
int main(void)
{
struct abc set[N];
srand(time(NULL));
for(int i=0; i<N; i++)
{
set[i].vec.x = rand();
set[i].vec.y = rand();
count_length(&set[i]);
}
}
void count_length(struct abc *_el)
{
for(int i=0; i<N; i++)
_el->length = sqrt(pow(_el->vec.x, 2.0) + pow(_el->vec.y, 2.0));
}
答案 0 :(得分:6)
保留函数声明
void count_length(struct abc *_el); /* compiler don't knows what is struct abc as you have defined it after this statement */
之后结构而不是之前。例如
struct vector {
double x;
double y;
};
struct abc {
struct vector vec;
double length;
};
void count_length(struct abc *_el); /* here compiler knows what is struct abc */