c - 在C中如何使用printf输出球坐标转换为笛卡尔坐标的值?

标签 c compiler-errors printf

我正在用 C 编写一个程序,其中程序的一部分应该将球面坐标转换为笛卡尔坐标,然后将它们显示在屏幕上,但我总是收到这部分的编译错误。我不确定我的语法是否错误,或者我是否遗漏了某些内容。有什么建议么?我认为问题在于我如何使用 printf,例如使用错误的参数或错误的参数数量。错误是:

Compilation error   time: 0 memory: 0 signal:0prog.c: In function 'print_test':
prog.c:21:9: error: incompatible type for argument 1 of 'printf'
  printf(cartesian.x);
         ^

这是根据错误消息我认为我做错的代码部分(我想我在代码中添加了一些东西,但大部分效果应该保持不变):

void print_test (struct threedCartesianData cartesian)
{
    printf(cartesian.x, cartesian.y)
}

我很确定我使用 printf 是错误的,但不确定到底是怎么回事。

最佳答案

  • printf() 添加格式字符串
  • printf()后添加分号
  • 声明 cart 并删除 ConvertSpclToCart() 中未使用的 cartesian
  • main() 中的 ConvertSphclToCart 更改为 ConvertSpclToCart
  • int main() 更改为 int main(void)
<小时/>
#include <stdio.h>
#include <math.h>
#define PI 3.14159


struct threedSphericalData
{
    float azimuth;
    float inclination;
    float range;
};

struct threedCartesianData
{
    float x;
    float y;
    float z;
};

void print_test (struct threedCartesianData cartesian)
{
    printf("%f %f\n", cartesian.x, cartesian.y);
}

struct threedCartesianData ConvertSpclToCart(struct threedSphericalData spherical)
{
    struct threedCartesianData cart;
    cart.x = spherical.range * cos(spherical.inclination * PI/180)*cos(-(spherical.azimuth)*PI/180);
    cart.y = spherical.range * cos(spherical.inclination * PI/180)*sin(-(spherical.azimuth)*PI/180);
    return cart;

}


int main (void)
{
    struct threedSphericalData spherical;
    spherical.azimuth = 35;
    spherical.inclination = 10;
    spherical.range = 300;
    struct threedCartesianData cartesian = ConvertSpclToCart(spherical);
    print_test(cartesian);
    return 0;
}

关于c - 在C中如何使用printf输出球坐标转换为笛卡尔坐标的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34316336/

相关文章:

swift - EXC_BAD_INSTRUCTION(代码 = EXC_I386_INVOP,子代码 = 0x0)

使用数字作为大小创建整数 char 数组

c - 打印语句更改字符数组输出

c - 如何在C中垂直打印?

c - testfork C 代码语法错误

c - <stdio.h> vs <math.h> - 为什么你必须链接一个而不是另一个?

c++ - 尝试运行已编译的 C++ 代码时出现段错误 11

angular - Bootstrap node_module的 Angular 错误

c - 为什么不需要释放静态数组?

c - 如何调用Oracle的olog()?