[] []的含义是否特定于上下文?(Is the meaning of [][] context

编程入门 行业动态 更新时间:2024-10-28 01:14:04
[] []的含义是否特定于上下文?(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 ?

更多推荐

本文发布于:2023-08-03 04:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1383912.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   含义   context   meaning

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!