为什么我在C++代码中得到地址而不是值
Why I get address instead of values , in C++ code?
我的代码从*.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;
所以你得到了你想要的:指针的值(例如地址)
Linie
、Coloana
和Valoare
是指针。
您必须在*
之前取消引用指针。
int value;
value = *m;
如果你想打印值,请再次点击此处:
cout<< *matR.Linie << " " << *matR.Coloana << " " << *matR.Valoare << endl;
所有类型为int *
的变量和类成员都应该是int
类型。目前,它们是未初始化的指针,而它们实际上是整数。
相关文章:
- 将数组的地址分配给变量并删除
- 空基优化子对象的地址
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 如何在c++程序中找到函数的地址
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 被解释为低级别const的const对象的地址
- 将地址分配给本地指针后,公共对象的变量将消失
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 内联程序集printf将整数解释为地址
- 为什么指针不写入类的地址?
- 如何在C++中获取该对象的类声明中对象的地址?
- 通过按地址访问变量
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 函数名是c中该函数的第一条指令的地址吗
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- ReadProcessMemory() 不适用于像 0x2840C6C68D8 这样的长地址
- CUDA:统一内存和指针地址的更改
- 当我们从/tp地址中添加/减去一个整数时会发生什么
- 如何使用 Boost Asio 在 Android 上获取我的本地 udp IP 地址?