1

In 'C', I have:

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

And I want to make a constant script for a regression test that contains multiple copies of My_Struct with initialized values, but also with the labels (aaa & bbb) for readability and convenience.

I can't figure out the syntax, or if this is even possible in C.

The closest I can get it is this:

struct
{

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

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

} const my_script_array;

But how do I define my_script_array to be an array of type My_Struct so that I don't have to cast it like (My_Struct)my_script_array in the code?

(COMPILER NOTE: I'm editing this software using Visual-C++, but it runs on Xcode 7.3 for ios, and ultimately it needs to compile on an ARM embedded processor.)

NEXT ATTEMPT:

The following code has no errors on Visual-C++ but gets Xcode error "expected ;" before the first '=' and also for the next dozen lines.

// 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
8
  • better say what version of C++ you are using, because initialization lists have gotten enhancements over the years. Commented Jul 12, 2016 at 15:02
  • 1
    You've tagged both C and C++. These are different languages, and you need to specify which one you're asking about. In the text you mention C, so maybe that's the one you mean, but I cannot be confident of that. Please edit your tags to clarify. Commented Jul 12, 2016 at 15:05
  • C and C++ are different languages! Commented Jul 12, 2016 at 15:05
  • This is one area where C and C++ differ considerably. Which language are you actually interested in? Commented Jul 12, 2016 at 15:05
  • 2
    @Olaf, at least an "are you really sure?" confirmation would be nice. Every once in a while there's a question that actually should be tagged with both. Commented Jul 12, 2016 at 15:07

4 Answers 4

6

If you want an array, as you seem to say, then you need to declare an array. You appear to instead be trying to define a struct whose members are of the struct type you are interested in. In C, you could define your array like so:

/* 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 }
                                                 };

Note the use of designated initializers for the struct members, presenting the member names as you said you wanted.

Sign up to request clarification or add additional context in comments.

5 Comments

This would be the solution, but in my Visual C++, the dots cause "error: expected an expression."
@DougNull, you asked about C. MSVC++ running in C mode is not a conforming C compiler, not even for 17-year-old C99. Not even for 26-year-old C90, for that matter, though it's pretty close there. Designated initializers in particular were introduced in C99, and MS does not support them. If you need to write C(ish) code as opposed to C++, and you're stuck with MSVC++, then Remy's variation is the best you can do, other than adding comments into the source.
@DougNull: Write a tool converting any convenient format to maintain the values into a source file and run this converter as your VC-project's Pre-Build Event.
@DougNull it might work in VS 2013 or later, as it is supposed to support C99 initializers.
(COMPILER NOTE: I'm editing this software using Visual-C++, but it runs on Xcode 7.3 for ios, and ultimately it needs to compile on an ARM embedded processor.) Eurika! It compiles on Xcode.
5
typedef struct
{
   int aaa;
   int bbb;
} My_Struct;

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

2 Comments

But, I need the data symbols. My actual structure has over 30 variables, and I want the symbols to make it easier when I want to alter the values in the script.
@DougNull you can't specify the field names when initializing them at compile time. If you want to access the names, you need to declare the array as non-const and then use runtime assignments to assign the individual fields as needed.
0

The fastest solution I used was to just copy and paste from the .h structure definition to the implementations declaration, and comment out the symbols. Readable, and easy to implement and modify. I perform functionality testing of my embedded software by scripts, so I do this a lot. Script testing allows extremely rigorous test of functionality because scripts can test for situations impossible to reproduce in lab.

Comments

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;
}

Might be a starting point. It might be more obvious to put them all into an array and know the index into the array that you need. Or pick out a pointer to the instances that you need to talk about.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.