从函数C ++返回float数组

Gal*_*ali 1 c++ arrays

我究竟做错了什么?

在第一个打印中,这些值很模糊,在第二个打印中,它们都是零。

第一次打印-q:0.965926 0.000000 0.258819 0.000000

第二次打印-q:0.000000 0.000000 0.000000 0.000000

float *AnglesToQaternion(float roll, float pitch, float yaw) { // Convert Euler angles in degrees to quaternions

    static float q[4];

    roll  = roll  * DEG_TO_RAD;
    pitch = pitch * DEG_TO_RAD;
    yaw   = yaw   * DEG_TO_RAD;

    float t0 = cosf(yaw * 0.5);
    float t1 = sinf(yaw * 0.5);
    float t2 = cosf(roll * 0.5);
    float t3 = sinf(roll * 0.5);
    float t4 = cosf(pitch * 0.5);
    float t5 = sinf(pitch * 0.5);

    q[0] = t0 * t2 * t4 + t1 * t3 * t5;
    q[1] = t0 * t3 * t4 - t1 * t2 * t5;
    q[2] = t0 * t2 * t5 + t1 * t3 * t4;
    q[3] = t1 * t2 * t4 - t0 * t3 * t5;

    printf(">> q-1: %f %f %f %f \n", q[0], q[1], q[2], q[3]);

    return q;
}

float q[4] = *AnglesToQaternion(0, 30.0, 0); 
printf(">> q-2: %f %f %f %f \n", q[0], q[1], q[2], q[3]);
Run Code Online (Sandbox Code Playgroud)

Cal*_*eth 6

根本不返回任何数组。

using degree = float;

struct Angles {
    degree roll;
    degree pitch;
    degree yaw;
};

struct Quaternion {
    float i;
    float j;
    float k;
    float l;
};

Quaternion angles_to_quaternion(Angles angles) 
{ 
    float yaw = angles.yaw * DEG_TO_RAD;
    float pitch = angles.pitch * DEG_TO_RAD;
    float roll = angles.roll * DEG_TO_RAD;

    float t0 = cosf(yaw * 0.5);
    float t1 = sinf(yaw * 0.5);
    float t2 = cosf(roll * 0.5);
    float t3 = sinf(roll * 0.5);
    float t4 = cosf(pitch * 0.5);
    float t5 = sinf(pitch * 0.5);

    return {
        t0 * t2 * t4 + t1 * t3 * t5,
        t0 * t3 * t4 - t1 * t2 * t5,
        t0 * t2 * t5 + t1 * t3 * t4,
        t1 * t2 * t4 - t0 * t3 * t5,
    };
}
Run Code Online (Sandbox Code Playgroud)