[] []的含义是否特定于上下文?(Is the meaning of [][] context-specific?)
我一直认为x [i]相当于*(x+i) 。 因此,x [i] [j]等效于*(*(x+i)+j) ,在这种情况下暗示必须将2D数组实现为指针数组,这些指针中的每一个都必须被解除引用。
但我了解到你可以这样在堆上创建一个2D数组:
char (*arr)[256]=malloc(512*256);如果前一假设是正确的,则arr [i] [j]将访问未经授权的位置(因为我们解除引用两次)。
在2d数组的情况下,我的前假设是错误的吗?
I always thought that x[i] is equivalent to *(x+i). So that would x[i][j] is equivalent to *(*(x+i)+j), which is in this case implies that a 2D array must be implemented as an array of pointers, each of these pointers has to be dereferenced.
But I learned that you can create a 2D array on the heap this way :
char (*arr)[256]=malloc(512*256);If the former hypothesis is right, then arr[i][j] would access an unauthorized location (since we are dereferencing two times).
Is my former hypothesis wrong in case of 2d arrays ?
更多推荐
发布评论