我正在尝试编译一个.c文件,但是gcc抛出了以下两个错误:
Polarkor.c:15:11: error: expected identifier or '('
pol2cart(double r, double phi, double *x, double *y){和
Polarkor.c:15:11: error: expected ')'这是我的密码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct cor { int *x; int *y; };
struct pol2cart(double r, double phi, double *x, double *y)
{
*(cor.x) = r * cos(phi);
*(cor.y) = r * sin(phi);
return cor;
}
int main(int argc, char *argv[])
{
double vecx, vecy = 4;
pol2cart(1.0, M_PI / 4, &vecx, &vecy);
printf("vec: x = %g, y = %g\n", vecx, vecy);
}有什么建议吗?
发布于 2015-05-02 16:19:07
我想你想:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct cor {int x; int y;};
struct cor
pol2cart(double r, double phi){
struct cor cor; /* legal, but generally a bad idea to use the same name */
cor.x = r * cos(phi);
cor.y = r * sin(phi);
return cor;
}在这种情况下,struct cor是一个对象类型,而cor是函数pol2cart中局部变量的名称。对变量使用与对象类型相同的名称通常被认为是不好的做法(确实很糟糕),但这是合法的,因为这两者在不同的名称空间中。(谁说C没有名称空间?!)看起来,您试图在原始代码中创建一个全局变量,该变量可以通过以下方式完成:
struct cor { int x; int y;} cor;但这可能是个坏主意,因为您的程序不太可能只使用一个坐标。看起来struct point可能是个更好的名字。
发布于 2015-05-02 16:18:55
struct pol2cart(double r, double phi, double *x, double *y) { /* ... */ }这看起来像是函数的不完整定义。它的返回类型是struct pol2cart (没有定义),并且没有名称。
也是
struct cor {int *x; int *y;};定义一个名为struct cor但没有变量的类型。在您的程序后面,您将引用一个名为cor的变量,该变量不存在。
发布于 2015-05-02 16:19:55
函数没有返回类型。
struct
pol2cart(double r, double phi, double *x, double *y){
//...也没有名字叫cor的物体。所以这些陈述
*(cor.x) = r * cos(phi);
*(cor.y)= r * sin(phi); 是无效的。您不能在这些构造中使用结构名称。您必须创建一个结构类型的对象来访问其数据成员。
此外,函数中不使用参数double *x和double *y。
这个程序没有任何意义。)
https://stackoverflow.com/questions/30004540
复制相似问题