1

I'm stuck with the above problem. Having an int8_t array:

int8_t i8array[3];
i8array[0] = 10;
i8array[1] = 15;
i8array[2] = 100;

And I want to not just convert but also reference an int16_t onto i8array[1]. So I want a new variable i16var, which is an int16_t with the value of 15<<8+100 = 3940.

And if I change i16var to 3941, it should change i8array's item#2 from 100 to 101.

Does ansi C has this facility? I had tried many things, and even in here, I just found answers for converting between these types.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Daniel
  • 2,318
  • 2
  • 22
  • 53
  • To avoid misunderstanding, i8array might have other items after #2 which is not interesting for i16var. – Daniel Jan 22 '17 at 18:13
  • You say you have tried many things. What have you tried? Add this to your question. – ad absurdum Jan 22 '17 at 18:25
  • 4
    What you are seeking to do is not doable reliably. All else apart, there are machines where `&i8array[1]` will not be correctly aligned to be treated as an `int16_t` pointer. You are assuming a big-endian layout in the memory; that may not be the way your CPU works. Also, you want two variables to refer to the same locations; you can only do that with a `union` or with pointers. You could try using `int16_t *i16p = (int16_t *)&i8array[1];` and then read using `int16_t i16 = *i16p;` and write using `*i16p = 3941;`, but that may or may not crash and may or may not produce the result you want. – Jonathan Leffler Jan 22 '17 at 18:30
  • 1
    Oh, and even if it works, it may or may not be efficient. It might involve reading and writing across word boundaries which, when it works at all, is slower than properly aligned data access. Any such coding is inherently fragile — all sorts of things can make it break, even if it happens to work right now. – Jonathan Leffler Jan 22 '17 at 18:32
  • Why do you want to do this? – Schwern Jan 22 '17 at 19:09
  • @JonathanLeffler Although OP's request seems odd, anything about [this answer](http://stackoverflow.com/a/41795050/2410359) do you think does not handle alignment, endian or other issues? – chux - Reinstate Monica Jan 23 '17 at 15:43
  • @chux: I don't think it handles endianness at all; it handles the alignment issue within the parameters set by the question. I'm not sure how realistic those constraints are, but that's a separate discussion. – Jonathan Leffler Jan 23 '17 at 16:06

1 Answers1

1

Alignment and endian are the key issues. The below should handle the alignment issue, and maybe the endian one. Good luck. Do not advise the coding approach.

int main(void) {
  union {
    struct {
      int8_t dummy;
      int8_t i8array[3];
    } view1;
    struct {
      int16_t dummy;
      int16_t i16var;
    } view2;
  } u;

  u.view1.i8array[0] = 10;
  u.view1.i8array[1] = 15;
  u.view1.i8array[2] = 100;
  printf("%d\n", htons(u.view2.i16var));
  u.view2.i16var = htons(3941);
  printf("%d\n", u.view1.i8array[2]);
  return 0;
}

Output

3940
101
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256