0

I have a headerfile which declares these arrays:

int stVal_On[] = {2};
int stVal_Off[] ={1};
int subVal_On[]={1};
int subMss[]={1};
int subVal_Off[]={0};

The dereferenced arrays are then used in structs that are declared:

enter image description here

Definition of WriteData struct:

/* Write structure used in loop for Read- and Write Tests */
typedef struct WriteData {
    char* name;                 // MMS object name
    const VOID* data;           // Data to write
    const SINT32 localFormat;   // SVI type (on server)
    const SINT32 dataLength;    // length of data to write/read
    const SINT32 NbrofElmnts;   // Number of elements to write/read
    char* description;          // SVI type as String (on server)
    char* SVI_Name;             // SVI address of the SVI mapped on server
    UINT32 svi_Length;          // length of SVI variable on server (used for readback)
} WriteData;

What is the purpose of this int arr[] = {1}; idiom? Why use arrays at all if only one value is assigned?

10
  • 2
    Hmm, you assume the programmer knew what he was doing. Finding this back in a .h file should not impress you. Use a telephone. Commented Mar 8, 2017 at 12:15
  • 2
    It seems more context is required. Commented Mar 8, 2017 at 12:18
  • 3
    Please add the definition of WriteData, too -- without that there's not much added information. Also please copy/paste code as code, not screenshots. Commented Mar 8, 2017 at 12:28
  • 2
    Another question is why an array pointer is passed to those functions and not a pointer to the first element (the decayed array). Identical result, but supports the "didn't know what he was doing" theory. Commented Mar 8, 2017 at 12:52
  • 3
    rule #1 of programming and driving a car: assume anyone else is a braindead monkey/psychopath (until proven otherwise). I have seen Code from "godlike" coders at our company, it was the living hell to read 6000 loc in one file all functions without parameters but working on global variables. What made him allegedly "godlike" was that he still knew what every line did and that it compiled on the first time and two years later he could fix things quite fast as he still knew where everything was. But the code was a massacre of coding standards.... and noone else could maintain it.Too bad he left Commented Mar 8, 2017 at 13:06

2 Answers 2

5

Well, one reason I can think of has to do with code organization. If you write your code in a tabular form:

struct {
  char const *file_name;
  uint16_t    flags;
  // Other data
} table [] = {
  { .file_name = "/usr/bin/foo", .flags = 0x0 },
};

for (size_t i = 0; i < sizeof(table)/sizeof(table[0]); ++i) {
  // do something meaningful with a table row table[i]
}

While it's just an array of 1 now, if you need to add more cases, your code is already written well for it. You'd just need to add another "row' to the table initializer.

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

Comments

4

All the definitions create arrays with only one element, true. The actual use case may vary.

One commonly used scenario is, this variable can be passed as a function argument and it will be possible to change the content of the array (the only element value) from the called function, which in case of a non-array variable is not possible. This maybe not the very smart way, but still, it's possible and someone chose to use it.

Also, as always, array size determined by the supplied initializer leaves the room for expansion without requiring major code changes.

3 Comments

For reference, this is what GMP uses for all of their types so you can pass them "by reference" to functions without scattering &'s everywhere.
@Quentin Right, read that somewhere, thanks for chiming in. :)
first, thanks for this valueable answer Sourav Ghosh. than thanks for this valueable comment @Quentin i think this is what the original dev intended, give me some time and i'll supply more info / cross check your hints!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.