-2

I am looking in to C code of my project and it mentioned as below. What does the statement nsq1[1] mean here at the end of the struct definition.

struct iec_apdu {
    unsigned char start;
    unsigned char length;
    unsigned short NS;
    unsigned short NR;
    struct iec_unit_id asduh;
    union {
        struct {
            unsigned short ioa16;
            unsigned char ioa8;
            iec_type1 obj[1];
        } sq1;

        struct {
            unsigned short ioa16;
            unsigned char ioa8;
            iec_type1 obj;
        } nsq1[1];
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
  • 3
    Read about [flexible array members](https://en.wikipedia.org/wiki/Flexible_array_member). You probably should use them. And this forum is not for learning purposes, you should take time to read some good C programming book, and also [documentation](http://en.cppreference.com/w/c) – Basile Starynkevitch Nov 24 '16 at 06:09
  • 1
    Your code is missing at least one `};`. Superficially, it looks like someone is trying to use the 'struct hack' in a union at the end of a structure definition. It's a bit hard to be sure because of the syntax error. If it is an attempt at a struct hack, then you should get it upgraded to use flexible array members. I'm glad it's you who has to suffer from that code; I would be in a state of loathing of whoever it was who tried to impose that on me. – Jonathan Leffler Nov 24 '16 at 06:12
  • `nsq1` is an array (of length 1) of anonymous `struct`-s (with fields `ioa16`, `ioa8`, `obj`). It is also a member of some anonymous `union`. – Basile Starynkevitch Nov 24 '16 at 06:12
  • http://stackoverflow.com/questions/8932707/what-are-anonymous-structs-and-unions-useful-for-in-c11 – Jay Nov 24 '16 at 06:46

2 Answers2

3

It is an array of 1 element of type struct nsq1. Something like: int x[1];

awakened
  • 192
  • 9
0
nsq1[1];

It is array of object you can access struct element using nsq1[0] and nsq1[1] since you have create two object for the struct

leuage
  • 566
  • 3
  • 17