[英]Get the pointer to an element in a two-dimensional array
如果我有这个数组:
#define a 10
#define b 20
int foo[a][b];
我可以这样获得foo[i][j]
的指针:
int *pointerToElement(i, j)
{
return *foo + i * b + j;
}
有没有使用索引符号( *foo[i][j]
)的简便方法?
这个怎么样:
return & foo[i][j];
您的方法是错误的:
*foo + i * b + j
这将取消对指针foo
引用,并为其值添加一些内容。 您想要的是:
foo + i * b + j
并且由于索引符号还取消了对指针的引用,因此您必须再次引用它:
&foo[i][j]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.