如何正确定义常量值的多个结构的常量数组?

时间:2016-07-12 15:00:01

标签: c arrays struct

在'C'中,我有:

typedef struct
{
   int aaa;
   int bbb;
} My_Struct;

我想为回归测试制作一个常量脚本,其中包含带有初始化值的My_Struct的多个副本,但也带有标签(aaa和bbb),以提高可读性和便利性。

我无法弄清楚语法,或者在C语言中是否可行。

我能得到的最接近的是:

struct
{

   struct {
   int aaa = 111;
   int bbb = 222;
   } first_script;

   struct {
   int aaa = 333;
   int bbb = 444;
   } second_script;

} const my_script_array;

但是如何将my_script_array定义为My_Struct类型的数组,以便我不必像代码中的(My_Struct)my_script_array那样强制转换它?

(编译器注意:我正在使用Visual-C ++编辑此软件,但它在ios的Xcode 7.3上运行,最终需要在ARM嵌入式处理器上编译。)

下一步尝试:

以下代码在Visual-C ++上没有错误,但是“预期”会出现Xcode错误;在第一个'='之前以及接下来的十几个行。

// Script of multiple reference summaries

struct
{
  struct
  {
    // WORDS:
    int total_words = 1;
    float all_absolute_impulse_gseconds = 2;  // gravity X seconds
    float all_average_absolute_impulse_gseconds = 3;
    float all_positive_impulse_gseconds = 4;
    float all_negative_impulse_gseconds = 5;

    float x_absolute_impulse_gseconds = 6;
    float x_average_absolute_impulse_gseconds = 7;
    float x_positive_impulse_gseconds = 8;
    float x_positive_average_impulse_gseconds = 9;

    float y_absolute_impulse_gseconds = 10;
    float y_average_absolute_impulse_gseconds = 11;

    float z_absolute_impulse_gseconds = 12;
    float z_average_absolute_impulse_gseconds = 13;

    float minimum_word_duration_seconds = 14;
    float average_word_duration_seconds = 15;
    float maximum_word_duration_seconds = 16;

    // EVENTS:
    int total_events = 17;
    int x_negative_transitions = 18;
    int x_zero_transitions = 19;
    int x_positive_transitions = 20;
    int y_negative_transitions = 21;
    int y_zero_transitions = 22;
    int y_positive_transitions = 23;
    int z_negative_transitions = 24;
    int z_zero_transitions = 25;
    int z_positive_transitions = 26;

    int total_comparison_attributes = 27; // set by  update_summary_attributes()
    int final_script_record = 0;
  } first;

  struct
  {
    // WORDS:
    int total_words = 28;
    float all_absolute_impulse_gseconds = 29; // gravity X seconds
    float all_average_absolute_impulse_gseconds = 30;
    float all_positive_impulse_gseconds = 31;
    float all_negative_impulse_gseconds = 32;

    float x_absolute_impulse_gseconds = 33;
    float x_average_absolute_impulse_gseconds = 34;
    float x_positive_impulse_gseconds = 35;
    float x_positive_average_impulse_gseconds = 36;

    float y_absolute_impulse_gseconds = 37;
    float y_average_absolute_impulse_gseconds = 38;

    float z_absolute_impulse_gseconds = 39;
    float z_average_absolute_impulse_gseconds = 40;

    float minimum_word_duration_seconds = 41;
    float average_word_duration_seconds = 42;
    float maximum_word_duration_seconds = 43;

    // EVENTS:
    int total_events = 44;
    int x_negative_transitions = 45;
    int x_zero_transitions = 46;
    int x_positive_transitions = 47;
    int y_negative_transitions = 48;
    int y_zero_transitions = 49;
    int y_positive_transitions = 50;
    int z_negative_transitions = 51;
    int z_zero_transitions = 52;
    int z_positive_transitions = 53;

    int total_comparison_attributes = 54; // set by  update_summary_attributes()
    int final_script_record = 0;
  } two;

  int final_script_record = true;

} const REFERENCE_SUMMARY_SCRIPT

4 个答案:

答案 0 :(得分:6)

如果你想要一个数组,如你所说,那么你需要声明一个数组。您似乎试图定义一个struct,其成员属于您感兴趣的结构类型。在C中,您可以像这样定义数组:

/* elements are unmodifiable: */               const
/* base type: */                               My_Struct
/* variable name and (implicit) dimension: */  my_script_array[]
/* initializer: */                             = {
    /* one element: */                             { .aaa = 111, .bbb = 222 },
    /* another: */                                 { .aaa = 333, .bbb = 444 }
                                                 };

请注意为结构成员使用指定的初始值设定项,按照您的说法显示成员名称。

答案 1 :(得分:5)

typedef struct
{
   int aaa;
   int bbb;
} My_Struct;

const My_Struct my_script_array[] = {
   {111, 222},
   {333, 444}
   // and so on ...
};

答案 2 :(得分:0)

我使用的最快解决方案是将.h结构定义复制并粘贴到实现声明中,并注释掉符号。可读,易于实施和修改。我通过脚本执行我的嵌入式软件的功能测试,所以我做了很多。脚本测试允许对功能进行极其严格的测试,因为脚本可以测试无法在实验室中重现的情况。

答案 3 :(得分:0)

#include <stdio.h>

typedef struct
{
   int aaa;
   int bbb;
} My_Struct;


const My_Struct first_script = {111, 222};
const My_Struct second_script = {333,444};

 struct 
{
   const My_Struct *first_script;
   const My_Struct *second_script;
} info = {&first_script,&second_script};

const My_Struct* my_script_array[] = 
{
   &first_script,
   &second_script
};

int main(void)
{
   int ii;

   printf("first {%d,%d}, second {%d,%d}\n",
      info.first_script->aaa,
      info.first_script->bbb,
      info.second_script->aaa,
      info.second_script->bbb);

   for (ii - 0; ii < sizeof(my_script_array)/sizeof(*my_script_array); ii++)
      printf("Entry %d {%d,%d}\n",
         ii,
         my_script_array[ii]->aaa,
         my_script_array[ii]->bbb);

   return 0;
}

可能是一个起点。将它们全部放入一个数组中并知道所需数组的索引可能更明显。或挑选一个指向您需要谈论的实例的指针。