使用 C 中的结构查找向量积

Find vector product using structures in C

我需要使用 C 中的结构编写函数来计算三个向量的向量积。我编写的函数可以正常工作,但我不知道如何打印结果。 结构对我来说是新的。

我收到一个错误:

format ‘%g’ expects argument of type ‘double’, but argument 2 has type ‘Vektor3d’ {aka ‘struct anonymous’}

#include <stdio.h>
typedef struct{
    double x,y,z;
}     Vektor3d;

Vektor3d vector_product(Vektor3d v1, Vektor3d v2)
{
    Vektor3d v3;
    v3.x=(v1.y*v2.z-v1.z*v2.y);
    v3.y=(v1.z*v2.x-v1.x*v2.z);
    v3.z=(v1.x*v2.y-v1.y*v2.x);
    return v3;
}

int main() {
   Vektor3d v1,v2;
    scanf("%lf %lf %lf", &v1.x, &v1.y, &v1.z);
    scanf("%lf %lf %lf", &v2.x, &v2.y, &v2.z);
    printf("%g", vector_product(v1, v2));
    return 0;
}

这一行:

    printf("%g", vector_product(v1, v2));

vector_product() returns Vektor3d 类型的对象。函数 printf() 不知道如何打印这个对象。您必须调用 printf() 并仅将它可以处理的类型传递给它(例如整数、双精度数等)

要解决此问题,只需将结果对象存储在一个变量中,然后将其组件传递给 printf()。也就是说,

int main() {
    Vektor3d v1,v2;
    Vektor3d v3;

    scanf("%lf %lf %lf", &v1.x, &v1.y, &v1.z);
    scanf("%lf %lf %lf", &v2.x, &v2.y, &v2.z);
    v3 = vector_product(v1, v2);          /* Save the return value in v3 */
    printf("%g %g %g", v3.x, v3.y, v3.z); /* pass the components of v3 to printf */
    return 0;
}