0

I want to be able to do this:

typedef int a[2], b[2], c[2];

without having to type the [2]. One solution is to do:

typedef struct { int a[2] } a, b, c;

but then you have to always do stuff like a.a[0] and that's no good.

5
  • So you're asking for advanced solution? Or determined to be able to do this? Commented May 21, 2016 at 17:24
  • typedef char[2] char_2; char_2 a,b,c; Commented May 21, 2016 at 17:25
  • 2
    a) I don't get why you are doing this. b) Chose C or C++, they have quite different type aliasing rules. Commented May 21, 2016 at 17:26
  • Is this different than a 2 dimensional array: int f[3][2]? With indices matching the variables, such as a==f[0][x] and b==f[1][x] and c==f[2][x]? Commented May 21, 2016 at 17:36
  • Do you want the syntax for a typedef for an array of 2 integers? Commented May 21, 2016 at 17:37

3 Answers 3

4

For C or C++98, a simple typedef will do:

typedef int int2[2];
int2 a, b, c;
Sign up to request clarification or add additional context in comments.

Comments

4

Well, the question is tagged with C++ and C, but there is a simple C++11 solution:

using int_arr = int[2];
int_arr a, b, c;

Comments

1

I would rather define a more reusable type and SIMD friendly i.e.

template<typename T, int size>
struct type_t {
    // 32-byte AVX aligned ready for [gnu] auto-vectorization 
    typedef T array_t[size] __attribute__((aligned(32)));
};

typedef type_t<int, 2>::array_t int_array2;
typedef type_t<double, 2>::array_t double_array2;

// and then
int_array2 a, b, c;
double_array2 d, e, f;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.