指向矩阵
我想声明一个指向矩阵并从矩阵检索回值的指针:
float *p;
float ar[3][3];
[..]//give values to ar[][]
p = ar;
//Keep on printing values in the 3 X 3 matrix
for (int i = 0; i < 10; i++)
{
p = p + i;
cout << *p << ", ";
}
I wanted to declare a pointer which would point to a matrix and retrieve a value back from the matrix:
float *p;
float ar[3][3];
[..]//give values to ar[][]
p = ar;
//Keep on printing values in the 3 X 3 matrix
for (int i = 0; i < 10; i++)
{
p = p + i;
cout << *p << ", ";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑你在:
也可以写成:
虽然你的
for
循环需要使用p = p + 1;
而不是p = p +我;
。如果您希望循环能够按行和列访问矩阵的成员,您还可以使用指向数组的指针:
I suspect that you are after:
which can also be written:
although your
for
loop then needs to usep = p + 1;
rather thanp = p + i;
.You can also use a pointer to an array, if you want your loop to be able to access the members of the matrix by row and column:
EDIT2:我是个白痴,我不小心使用了
float **matrix
而不是float (*matrix)[3]
。咖啡馆一直都有正确的答案。这是你想要的吗?
编辑:您还可以:
EDIT2: I'm an idiot I accidentally had
float **matrix
instead offloat (*matrix)[3]
. caf had the right answer all along.Is this what you want?
EDIT: you can also have: