-2

I'm looking into an example application and I found this literal:

#define OFS_INIT            {{(UINT16)0u},{(UINT16)~(UINT16)0u}}

void main( void )
{
    LOCAL_STATIC(, RDS_UINT16,  u16_msgOfs, OFS_INIT);
}

I don't have the definition of LOCAL_STATIC and I'm tring to understand how it works with OFS_INIT.

underscore_d
  • 6,309
  • 3
  • 38
  • 64
Federico
  • 1,117
  • 6
  • 20
  • 37
  • 1
    Do you know what the `~` operator does? – dbush Apr 06 '21 at 13:30
  • @dbush no I mean the C pre-processor output. – Federico Apr 06 '21 at 13:31
  • 3
    There is nothing preprocessor specific here except the `#define`. It is simply replacing `OFS_INIT` with the thing on the right which looks like an array or `struct` initializer. – Eugene Sh. Apr 06 '21 at 13:31
  • 1
    All the preprocessor will do is replace `OFS_INIT` with `{{(UINT16)0u},{(UINT16)~(UINT16)0u}}`. – dbush Apr 06 '21 at 13:33
  • 1
    Your code after the edit looks invalid. Is it a real code? `OFS_INIT` only makes sense as an initializer. – Eugene Sh. Apr 06 '21 at 13:33
  • If you search the code for where `OFS_INIT` is used it will probably make more sense. – dbush Apr 06 '21 at 13:34
  • @EugeneSh: you get the point! Could you please explain how the macro is used to initialize an array or a struct? – Federico Apr 06 '21 at 13:34
  • It looks like a `struct` initializer which would make sense. Initializing a `uint16_t` with that does not make sense. – mediocrevegetable1 Apr 06 '21 at 13:35
  • 3
    It is using the same syntax as `{{0}, {0}}`, just with a bunch of casts and the negation operator. – Eugene Sh. Apr 06 '21 at 13:35
  • @EugeneSh. the second would probably be `USHORT_MAX` – mediocrevegetable1 Apr 06 '21 at 13:36
  • 1
    @mediocrevegetable1 Yeah, I just show the syntax, not the resulting values – Eugene Sh. Apr 06 '21 at 13:39
  • @EugeneSh. Thanks. I updated the question. And just for my understanding, why I would like to iniatilize a struct element {(UINT16)~(UINT16)0u}? – Federico Apr 06 '21 at 13:39
  • 1
    We don't know what your `struct` is. Show it, so we don't guess – Eugene Sh. Apr 06 '21 at 13:41
  • 1
    @Federico depends entirely on what `LOCAL_STATIC` does with it, and what exactly the `struct` definition is. – mediocrevegetable1 Apr 06 '21 at 13:41
  • @mediocrevegetable1 unfortunately I don't have the full code and I'm tring to understand what the programmer did. In detail I don't know what "RDS_UINT16" is. I think it is a uint16_t but in this case, why I should initialize it with {{(UINT16)0u},{(UINT16)~(UINT16)0u}}? – Federico Apr 06 '21 at 13:44
  • *"I don't have the definition of LOCAL_STATIC"* - Compile with -E and then you have it – klutt Apr 06 '21 at 14:02
  • *"I don't have the full code and I'm tring to understand what the programmer did."* - You should ask the programmer directly instead. It's very hard to just guess such things. Especially since the code can be wrong. – klutt Apr 06 '21 at 14:04
  • This is incredibly bad code. I would strongly recommend staying clear of it. If you want a code review of this little snippet I can give one - it won't be kind. But that doesn't answer your question, so... – Lundin Apr 06 '21 at 14:04

1 Answers1

1

Looks like LOCAL_STATIC is a macro that can be used to declare a variable of a struct type whose fields are typed with one of the argument and initialised with a given value.

Probably this would declare something like:

some_struct_type_related_to_RDS_UINT16 u16_msgOfs = OFS_INIT;

where OFS_INIT defines the initial value of the struct : { {0}, {0xFFFF} }.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • Thanks! It is clear now. – Federico Apr 06 '21 at 13:48
  • "is a macro that can be used to declare a variable of a struct type" Or it's some goo in the form of (multi-dim) arrays, compound literals, who knows. That's why we shouldn't invent secret macro languages but stick to C, since the latter can actually be read by C programmers. – Lundin Apr 06 '21 at 14:06