我可能遗漏了一些基本的东西,但我找不到以下问题的解决方案。 我有一些浮点元素的二维数组,我试图找到一种方法来仅使用单个值来引用它们。 示例:
float test[5][50];
test[3][25] = 34.67;
cout << test[3][25] << endl;
int id = 25;
cout << *test[id*5+3] << endl;
我希望从两个 cout 中得到相同的结果。相反,我的输出看起来像这样:
34.67
Segmentation fault
我做错了什么?
谢谢!
最佳答案
未经测试,我认为这样的方法可能有效。请注意,C++ 数组从左维度到右维度是 major->minor。
float test[5][50];
test[3][25] = 34.67;
cout << test[3][25] << endl;
int id = 25;
float* test2 = &test[0][0]
cout << test2[(3 * 50) + id] << endl;
关于c++ - 使用单个值引用二维数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5170736/