What are the differences between an array of char pointers and a 2D array?

前端 未结 4 1107
一向
一向 2021-01-06 18:38

What are the differences between an array of char pointers and a 2D array?

4条回答
  •  爱一瞬间的悲伤
    2021-01-06 19:13

    Array of arrays (aka multi-dimensional array) looks like (in memory):

    a[0][0], a[0][1], a[0][n-1], a[1][0], a[1][1], ..., a[1][n-1], ..., a[m-1][n-1]
    

    array of pointers looks like:

    p[0], p[1], ..., p[m-1]
    

    where each slot is a pointer and can point to whatever. If they all happen to point to arrays with n elements each, then p[i][j] and a[i][j] can be used similarly in expressions, but they're actually quite different objects.

提交回复
热议问题