gpt4 book ai didi

c++ - 为什么我在 C++ 代码中得到地址而不是值?

转载 作者:行者123 更新时间:2023-11-28 05:41:49 27 4
gpt4 key购买 nike

我的代码从 *.mtx 文件中读取一个稀疏矩阵,并且应该在控制台打印该矩阵(仅用于测试,对于实际情况我想返回稀疏矩阵),但他打印的是地址而不是值。

我的代码:

#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

因此,正如您在我的输出中看到的那样,它打印的是地址,而不是值。非常感谢!

最佳答案

您将以下成员声明为指向 int 的指针:

int *Linie, *Coloana, *Valoare;

然后打印这些指针:

cout<<matR.Linie << " " << matR.Coloana << " " << matR.Valoare <<endl;

所以你得到了你所要求的:指针的值(例如地址)

关于c++ - 为什么我在 C++ 代码中得到地址而不是值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36938819/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com