0

I have four arrays:

int a1 [3] = { 10, 20, 30 }; 
int a2 [3] = { 10, 20, 30 }; 
int a3 [3] = { 10, 20, 30 };
int a4 [3] = { 10, 20, 30 };

I want to call array depending on a global variable:

int sys=1;

lets say:

int a1+sys; // this should gives array a2
int a1+2*sys; // this should gives array a3

How can I achieve this ?

3
  • you can't. Why do you think you need this? Commented Jul 9, 2021 at 14:33
  • 2
    Instead of individual 1D arrays, set up a single 2D array: int a[4][3] = { { 10, 20, 30 }, { 10, 20, 30 }, ...}; Now you can write a[sys]. Commented Jul 9, 2021 at 14:34
  • 2
    Maybe a 2D array: int a[4][3] = { {10,20,23}, {.... and then a[sys][x] ?? Commented Jul 9, 2021 at 14:35

1 Answer 1

1

It seems that what you're looking for are arrays of arrays:

int a[][3] = {
    { 10, 20, 30 },
    { 10, 20, 30 },
    { 10, 20, 30 },
    { 10, 20, 30 },
}; 

auto& a2 = a[sys];
auto& a3 = a[2*sys];
Sign up to request clarification or add additional context in comments.

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.