从函数返回结构数组 - C编程

时间:2014-04-15 17:50:56

标签: c arrays function struct

所以我试图编写一个返回多个值数组的函数。目前,它正在正常运行,但只输出最终的计算值。我怎样才能使输出包含所有计算值?

我的代码如下所示:

//Practice to output an array of structs

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct boat_params {
    double V, Uc, Vc;
    };

struct boat_params submerged_volume(double L1, double L2, double Lavg, double H) {

    struct boat_params volume;

    double V_sub, Uc_sub, Vc_sub;
    V_sub = 0;

    //Boat description
    double C, delta;
    double theta, theta_rad, theta_min, theta_min2, theta_lim, theta_lim2, theta_lim_deg;
    double Ug1, Ug2, Vg1, Vg2, V1, V2;
    double pi; 

    pi = 4*atan(1);
    C = sqrt(L1*L1 + L2*L2);
    delta = acos(L1/C);

    theta_lim = asin(H/L1);
    theta_lim_deg = (theta_lim/pi) * 180.0;
    theta_min = asin(H/C) - delta; 
    theta_min2 = 0;

    //Calculating the submerged volume and centre of gravity for each different angle
    for (theta = 0; theta <= 10; theta ++) {

    //**Note: I've taken out the actual calculations of V_sub, Uc_sub, and Vc_sub for brevity**


        volume.V = V_sub;
        volume.Uc = Uc_sub;
        volume.Vc = Vc_sub;
        }


    return volume; 

    }


int main () {

double L1, L2, Lavg, H;
struct boat_params volume; 

L1 = 17.6; 
L2 = 3; 
Lavg = 4;
H = 4.5;

volume = submerged_volume(L1, L2, Lavg, H);

printf("V = %lf\nUc = %lf\nVc = %lf\n", volume.V, volume.Uc, volume.Vc);

return 0;

}

我可以让它正确输出最后计算的值(对于theta = 10),但这是我得到的唯一值。我如何为每个theta值计算V_sub,Uc_sub和Vc_sub?并输出每个值。我假设这意味着将结构转换为数组,并使用该结构的值填充数组的每个元素,但我不知道如何执行此操作!

我非常感谢您的帮助,并提前感谢您。

另外:如果可能的话,我想避免使用指针,但要明白这可能是不可能的!我还很新,不擅长使用它们!

2 个答案:

答案 0 :(得分:0)

试试这个,只需将你的逻辑添加到循环和数学中:

#include <stdio.h>
#include <stdlib.h>

#define ARRSIZE 100

typedef struct boat_params {
  double  V, Uc, Vc;
} Volume;

struct boat_params submerged_volume(double L1, double L2, double Lavg, double H, Volume *volumes[]) {
  double theta;

  int i = 0; /* only example, change as needed */
  Volume *p;
  for (theta = 0; theta <= 10; theta ++) {
    p = malloc(sizeof(* p)); 
    if (p == NULL) {
      printf("malloc failed to allocate a new space");
      exit(0);
    }                   

    p->V = 1; //V_sub;
    p->Uc = 2; //Uc_sub;
    p->Vc = 3; //Vc_sub;

    volumes[i] = p;
    i++;
  }

}


int main () {
  double L1, L2, Lavg, H;

  L1 = 17.6; 
  L2 = 3; 
  Lavg = 4;
  H = 4.5;

  Volume *volumes[ARRSIZE];

  submerged_volume(L1, L2, Lavg, H, volumes);
  printf("V = %lf\nUc = %lf\nVc = %lf\n", volumes[0]->V, volumes[0]->Uc, volumes[0]->Vc); /* first element for example */

  return 0;
}

如果您事先不知道卷数组的大小,则应考虑使用链表。

答案 1 :(得分:0)

你是对的,你需要有一个阵列。如果数组中的元素数量是常量,您还可以创建一个包含该数字元素的结构,但请不要这样做。
要对阵列进行操作,不幸的是,你需要指针。在C中执行此操作的一种非常常见的方法是不返回指针,而是传入“结果”指针。这意味着它将由函数的用户分配空间并释放它,他也可以使用数组的语法。在您的代码中,似乎值的数量是不变的,这使得上述解决方案成为可能。或者你可以在堆上分配空间(使用malloc)并返回一个指针,但这意味着用户需要释放他从未分配过的内存,反直觉并且如果忘记这样做可能会导致内存泄漏。请考虑以下解决方案:

void submerged_volume(double L1, double L2, double Lavg, double H, struct boat_params *result) {
    // your calculations here
    for (theta = 0; theta <= 10; theta ++) {
        (result+theta)->V = V_sub;
        (result+theta)->Uc = Uc_sub;
        (result+theta)->Vc = Vc_sub;
    }
}

// somewhere in your code where you want to use your function
struct boat_params values[11];
unsigned char i = 0;
submerged_values(/* parameters */, values);
for (; i <= 10; ++i) {
    printf("V = %lf\nUc = %lf\nVc = %lf\n", values[i].V, values[i].Uc, values[i].Vc);
}