Khalaf90
Khalaf90

Reputation: 25

How to use a global variable as part of an array name

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 ?

Upvotes: 0

Views: 96

Answers (1)

eerorika
eerorika

Reputation: 238351

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];

Upvotes: 1

Related Questions