将指针传递给字符串作为函数的参数时发生类型冲突

时间:2019-04-12 05:37:16

标签: c pointers struct

Error description正如我在主题中所写的那样,当我尝试将指针传递给使用结构体数组构造的结构体时,我遇到了冲突类型错误。您是否有删除此错误的建议?我想念的是什么?

#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));
}

1 个答案:

答案 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 */