简体   繁体   中英

C++ multidimensional arrays

I'm new to c++ and its developing. i used

static const int ipx[7][2] = { {-1, 0}, {-1, -1}, {-1, 1}, {-2, 0}, {-2, -1}, {-2, 1}, {0, 0} };

and when i prints the values of that array i got results as follows. could anyone please explain why this happens. Thank you

printf("-> %i \n",ipx[3][1]); // prints -> 0
printf("-> %i \n",ipx[7][1]); //prints-> 28 
printf("-> %i \n",ipx[7][0]); //prints ->-> 1 
printf("-> %i \n",ipx[5][1]);  //prints -> 1 

thank you in any advance.

C++ arrays are 0-indexed, so printing out ipx[7][0] is undefined behaviour since the indices run 0..6 for a 7-element array. You probably wanted ipx[2][1] , ipx[6][1] , ipx[6][0] , and ipx[4][1] .

基本上,如果您声明一个具有7个“插槽”的数组,则可以使用插槽0-6。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM