我的代碼從* .mtx文件中讀取稀疏矩陣,並且應該在控制檯上打印矩陣(僅用於測試,對於真實情況我想返回稀疏矩陣),但是他打印的地址不是值。爲什麼我用C++代碼獲取地址而不是值?
我的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
struct MatriceRara
{
int *Linie, *Coloana, *Valoare;
int nrElemente, nrLinii, nrColoane;
};
MatriceRara Read(const char* mtx) {
const char * mtx_file = mtx;
ifstream fin(mtx_file);
MatriceRara matR;
int nrElemente, nrLinii, nrColoane;
// skip header:
while (fin.peek() == '%') fin.ignore(2048, '\n');
// read parameters:
fin >> nrLinii >> nrColoane >> nrElemente;
matR.nrElemente = nrElemente;
matR.nrLinii = nrLinii;
matR.nrColoane = nrColoane;
cout << "Number of rows: " << matR.nrLinii <<endl;
cout << "Number of columns: " << matR.nrColoane << endl;
cout << "Number of not null values: " << matR.nrElemente << endl;
for (int i = 0; i< nrElemente; i++)
{
int *m ,*n,*data;
fin >> (int &) m >> (int &) n >> (int &) data;
matR.Linie = m;
matR.Coloana = n;
matR.Valoare = data;
//only for test:
cout<<matR.Linie << " " << matR.Coloana << " " << matR.Valoare <<endl;
}
//return matR;
}
int main() {
MatriceRara a = Read("Amica.mtx");
}
我的輸出:
Number of rows: 5
Number of columns: 5
Number of not null values: 8
0x7fff00000001 0x7f4400000001 0x1
0x7fff00000000 0x7f4400000001 0x1
0x7fff00000000 0x7f4400000001 0x1
0x7fff00000000 0x7f4400000001 0x1
0x7fff00000000 0x7f4400000001 0x1
0x7fff00000000 0x7f4400000001 0x1
0x7fff00000000 0x7f4400000001 0x1
0x7fff00000000 0x7f4400000001 0x1
所以,你可以在我的輸出中看到的,這是打印的地址,而不是值。 非常感謝!
因爲你輸出指針。 – drescherjm
'int * m,* n,* data; (int&)m >>(int&)n >>(int&)data;'是未定義的行爲。 – drescherjm
@drescherjm我也希望。我認爲OP會盡量減少代碼。 – fjardon