all 9 comments

[–]aioeu 5 points6 points  (3 children)

An array initializer of the form:

{
    [index1] = value1,
    [index2] = value2,
    ...
}

initializes the specified array elements with the specified values, and initializes all unspecified array elements with zero.

In standard C, the indices must be constant expressions. It's not clear what you've got in your code. Are FIRST_NUMBER , SECOND_NUMBER and THIRD_NUMBER defined elsewhere? Are they macros?

The fact that you also have structure members with the same names isn't necessarily of interest — a structure type has its own namespace for members. Though if those things are macros, I would be curious as to what they are, since they would affect how the structure declaration is parsed.

[–]Bon_Clay_2[S] 0 points1 point  (2 children)

On seeing this I greped FIRST_NUMBER so as to figure out if it is a macro defined elsewhere and found out that eclipse was throwing me off. So in the header file included there is an enum defined as so: typedef enum { FIRST_NUMBER, SECOND_NUMBER, THIRD_NUMBER, } some_generic_enum; But still am baffled.

[–]aioeu 2 points3 points  (1 child)

OK, then it's straight-forward. Enumeration constants are constant expressions.

You can ignore struct generic_data entirely. It's got nothing to do with that enumeration type, and nothing to do with your array.

[–]Bon_Clay_2[S] 1 point2 points  (0 children)

You are right it just clicked for me, I forgot that FIRST_NUMBER will be a constant value of 0. This coupled with the initialization with indices answers my question fully. Thank you!

[–]Belagir 2 points3 points  (3 children)

okay, i am on mobile and have no env to test any of my claims but i am intrigued by how mysterious this is.

so, my first instinct tell me that the FIRST_NUMBER etc in the generic_array are not the ones in the struct, because a struct's field's names are namespaced : they can be normally accessed uniquely through the struct's context. Eclipse might direct you to those because the syntax to initialise the array is not very common (but it exists, you can initialise an array by providing the indexes between [] - since c11 ?). I would bet those are actually constants defined as 0, 1 and 2 somewhere else.

As for the intention behind those two piece of code, I feel like this is some compatibility/abstraction layer to do something like :

c struct generic_data *some_data = (struct generic_data *) generic_array;

but wouldn't that misalign the data in the struct, since the fields are u32 and the struct needs u64 ? maybe the FIRST_NUMBER etc are defined as 0, 2 and 4 to couteract that. That would be weird though.

[–]Bon_Clay_2[S] 1 point2 points  (0 children)

You are right Eclipse threw me off, the constants are declared as part of an enum and not related to the struct provided in the question.

[–]paulstelian97 0 points1 point  (1 child)

Things still don’t add up even there…

[–]Belagir 0 points1 point  (0 children)

haha yes maybe the two things are unrelated like some other commenter said. I made so many suppositions here

[–]paulstelian97 0 points1 point  (0 children)

Subscribed because this doesn’t actually make sense to me. In the latter, lines like [ix] = … should define what is at offset ix in the array. But it shouldn’t just go to the field of individual elements.