将struct中的变量调用到c中的函数中的问题

时间:2011-04-11 04:24:25

标签: c

struct
{
    float lat_radians;          //latitude in radians
    float lon_radians;          //longtiude in radians
    float alt_radians;          //altiude in radians
    double ECEF_X;          // ECEF X in metres
    double ECEF_Y;                // ECEF Y in metres
    double ECEF_Z;                // ECEF Z in metres

} gps_position;

void main()
{
LatLongAlt_to_ECEF_coordinates(struct gps_position {float lon; float lat; float alt} arg);
}

我有来自c141的错误:'struct'附近的语法错误。我试图想,但找不到任何解决方案
请帮忙 .. 感谢

祝你好运 凯文


感谢您提供的有用信息。但我对我的代码做了一些更改,我仍然遇到错误C3861的问题:'lla_to_ECEF':找不到标识符&错误C2365:'lla_to_ECEF':重新定义;以前的定义是'以前未知的标识符'我可以知道如何从这里纠正我的错误?谢谢凯文

#include <stdio.h>

struct gps_position
{
float alt;
float lon;
float lat;
double ECEF_x;
 };

 void main ()
 {

lla_to_ECEF (gps_position.alt);

 return;
 }

 float lla_to_ECEF (float alt)
 {

 //some calculate to be done

 }

4 个答案:

答案 0 :(得分:4)

您需要在struct之后立即声明。有关示例,请参阅您最喜欢的C Book。

struct gps_position
{
    float lat_radians;          //latitude in radians
    float lon_radians;          //longtiude in radians
    float alt_radians;          //altiude in radians
    double ECEF_X;              // ECEF X in metres
    double ECEF_Y;              // ECEF Y in metres
    double ECEF_Z;              // ECEF Z in metres
} ;

typedef struct 
{
      float lat_radians;          //latitude in radians
      float lon_radians;          //longtiude in radians
      float alt_radians;          //altiude in radians
      double ECEF_X;              // ECEF X in metres
      double ECEF_Y;              // ECEF Y in metres
      double ECEF_Z;              // ECEF Z in metres
} gps_position ;

答案 1 :(得分:1)

首先,这不是声明结构的有效方法,试试这个:

struct gps_position
{
    float lat_radians;          //latitude in radians
    float lon_radians;          //longtiude in radians
    float alt_radians;          //altiude in radians
    double ECEF_X;              // ECEF X in metres
    double ECEF_Y;              // ECEF Y in metres
    double ECEF_Z;              // ECEF Z in metres

};

其次,如果要将结构传递给函数,请执行:

gps_position arg;

//Do assigning stuff before the function is called.

LatLongAlt_to_ECEF_coordinates(arg);

答案 2 :(得分:1)

您希望首先为gps_position结构提供标记:

struct gps_position
{
    float lat_radians;          //latitude in radians
    float lon_radians;          //longtiude in radians
    float alt_radians;          //altiude in radians
    double ECEF_X;          // ECEF X in metres
    double ECEF_Y;                // ECEF Y in metres
    double ECEF_Z;                // ECEF Z in metres

};

您在定义名为gps_position的变量之前所拥有的,但没有给结构本身命名。

void main()

并非它与手头的问题特别相关,但main通常应返回int

{
LatLongAlt_to_ECEF_coordinates(struct gps_position {float lon; float lat; float alt} arg);
}

如果你想用gps_position调用一个函数,你通常会这样做:

struct gps_position pos;

LatLongAlt_to_ECEF_coordinates(pos);

仅在典型情况下,您希望将指针传递给结构:

LatLongAlt_to_ECEF_coordinates(&pos); 

我个人认为我将基于弧度的坐标与基于ECEF的坐标分开:

struct radian_coords { 
   float latitude, longitude, altitude;
}

struct ECEF_coords { 
   double X, Y, Z;
}

然后,当您从一个转换为另一个时,您将传递每个实例:

struct radian_cords r_pos = { 12345.6, 23456.7, 123.4 };
struct ECEF_coords e_pos;

LatLongAlt_to_ECEF_coordinates(&r_pos, &e_pos);

或者,您可以编写函数以返回正确的类型:

e_pos = LatLongAlt_to_ECEF_coordinates(&r_pos);

在这种情况下,您的函数标题看起来像:

struct e_pos LatLongAlt_to_ECEF_coordinates(struct r_pos const *input) { 
    struct e_pos ret;
    // compute and assign values to ret.X, ret.Y and ret.Z
    return ret;
}

答案 3 :(得分:0)

这将消除您的错误:

这是将结构作为参数传递给函数

的正确方法
     #include <stdio.h>
     typedef struct 
     {
        float lat_radians;          //latitude in radians
        float lon_radians;          //longtiude in radians
        float alt_radians;          //altiude in radians
        double ECEF_X;          // ECEF X in metres
        double ECEF_Y;                // ECEF Y in metres
        double ECEF_Z;                // ECEF Z in metres

      } gps_position;

    void main()
    {
        gps_position gps;
            LatLongAlt_to_ECEF_coordinates(gps);
    }