0

I would like to be able (if possible) to, through a single variable, access both an array and struct members. The closest I got was the following:

typedef union {
    struct {
        float x;
        float y;
        float z;
    };
    float getItem[3];
} Vector3D;

However, if things are done this way, everytime I want to access a Vector3D variable as an array, I need to do it like this:

Vector3D vec;
vec.getItem[0] = 3.5f; //same as vec.x = 3.5f;
vec.getItem[1] = 8.4f; //same as vec.y = 8.4f;
vec.getItem[2] = vec.getItem[0] + vec.getItem[1]; //same as vec.z = vec.x + vec.y

I would like to know if there is any way, with any combination of typedefs, structs, unions and pointers (or with any other tool that C provides), to use the variable as both a struct and a pointer to that struct, to be accessed as an array directly, like this:

Vector3D vec;
vec[0] = 3.5f;
vec.y = 8.4f;
vec[2] = vec.x + vec[1];

I've come across a few solutions to similar problems, such as this one, but in that question, he suggests to create another variable which is a pointer to the array, whereas I would like to access both the array and the struct with the same variable, with only one declaration (as in my example). Is there any way to do it?

Diego
  • 1
  • 1
  • No there isn’t. To elaborate: a variable has a single type, but you want it to have whatever of the type is determined by the method you use to access it. Nada. – DisappointedByUnaccountableMod Apr 14 '19 at 13:14
  • You would do pointer arithmetic, but it will be more verbose and error prone than the solution that you already have – geckos Apr 14 '19 at 13:19
  • No. The way you're currently doing it makes a combination of 3 `float` variables into a pointer to a float (suggested by the array). – S.S. Anne Apr 14 '19 at 14:00
  • @JL2210 I see no pointers. Just an array. Very different concepts. – alx - recommends codidact Apr 14 '19 at 14:44
  • 1
    There is a way, if you use slightly different names. Don't use it, it hides information, and has no benefit, but as you asked... `#define veca vec.getItem` Really, don't do this. – alx - recommends codidact Apr 14 '19 at 14:44
  • You still have the problem of padding. If the struct has any padding in between, you are screwed. – alx - recommends codidact Apr 14 '19 at 14:47
  • @CacahueteFrito then, if I'm going to use it as it is (with the `getItem` member), would you reccomend to explicitly pack it? – Diego Apr 14 '19 at 15:01
  • Pack it? I don't understand it. – alx - recommends codidact Apr 14 '19 at 15:13
  • @CacahueteFrito packing the struct prevents the compiler to do any padding on it, as explained in [here](https://stackoverflow.com/questions/4306186/structure-padding-and-packing). However, as far as I know, this is not necessary in my case, since the struct only holds variables of the same type. – Diego Apr 14 '19 at 15:17
  • @Diego Yes, do it explicitly. The compiler will not add padding normally, but it could. However, if you don't have a good reason to use this union, I would choose either the array or the struct. – alx - recommends codidact Apr 14 '19 at 15:20

0 Answers0