Treating 2D array as 1D array

后端 未结 4 1925
余生分开走
余生分开走 2020-12-10 12:31

Let\'s say we have a 2D int array:

int a[3][4] = { { 1,3,2,4 }, { 2,1,5,3 }, { 0,8,2,3 } };

Is it legal and valid to take its

4条回答
  •  离开以前
    2020-12-10 13:01

    From http://www.cplusplus.com/doc/tutorial/arrays/

    int jimmy [3][5];   // is equivalent to
    int jimmy [15];     // (3 * 5 = 15)  
    

    when creating an array ( of any dimension ) the array memory is a fixed block of memory in size = sizeof(type) * dim0 * dim1 * ....;

    So to your question, yes you can recast the array safely into one dimensional array.

提交回复
热议问题