How to access any variable name according to loop index

前端 未结 5 1816

I have some integer variables, I named them n0 to n9. I want to access them using a loop. I tried this code to do that:

int n0 = 0, n1          


        
5条回答
  •  情歌与酒
    2021-01-29 17:31

    The right way to do it is to declare an integer array and not 10 different variables:

    int n[10];
    

    Now you can access 10 int variables with n[0] through n[9].

提交回复
热议问题