打印 int 指针的值
我一直在努力解决这个问题,但我似乎无法让它正常工作。我正在返回指针列表的最后一个值,并且我想打印它,但它正在打印一个非常随机的数字。我假设这是指针的内存地址,但是当我取消引用它时,我的输出仍然做同样的事情。
我的 Pointerlist 是一个指针列表,例如: list
例如,这是我的方法返回:
int* end() { return (pointerList.back()); }
这就是我调用它的方式
int* totry = ca.end();
cout << *totry;
这是打印内存地址而不是值。有谁有任何想法如何解决这个问题?
提前致谢!
编辑: 这是 int 指针所指向的内容: 我有一个值列表,例如 [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
我有一个指向该列表不同部分的指针列表,如下所示:
[0,4,8,12]
我有代码: int* end() { return (pointerList.back()); }
在我的头文件中,以及在我的 .cpp 文件中的调用:
int* totry = ca.end(); 计算<< *尝试;
这就是我声明指针列表的方式,
class ptrList
{
public:
std::list<value_type> listOfValues;
std::list<*int> pointerlist;
我将列表指针填充到“add”函数中,然后这样做:
int lstsqrt = 4;
for (int a = 1; a < lstsqrt; a++)
{
int endptr = a + (int)lstsqrt;
pointerlist.push_back((&*listOfValues.begin() + endptr)); //( (lstsqrt - 1) + a) );
}
这是我的 end() 方法
int* end() {return (pointerlist.back());}
然后将其传递给我的 toTry 变量。
I have been working on this and I can't seem to get this working properly. I am returning a pointer list's last value, and I would like to print it, but It is printing a very random number. I assuming that this is the memory address of the pointer, but when I dereference it, my output still does the same thing.
My Pointerlist is a list of pointers, like: list<int*> pointerList
For example, this is my method returning :
int* end() { return (pointerList.back()); }
An this is how I am calling it.
int* totry = ca.end();
cout << *totry;
This is printing the Memory Adress and not the value. Does anyone have any Ideas how to solve this?
Thanks in advance!
EDIT:
Here is what the int pointers are pointing to:
I have a list of values such as [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
And I have a list of pointers that points to different parts of that list like the following:
[0,4,8,12]
I have the Code: int* end() { return (pointerList.back()); }
in my Header file, and the call in my .cpp file:
int* totry = ca.end();
cout << *totry;
This is how I declare my pointerlist
class ptrList
{
public:
std::list<value_type> listOfValues;
std::list<*int> pointerlist;
I fill my list pointers inside an "add" function, and I do it like this:
int lstsqrt = 4;
for (int a = 1; a < lstsqrt; a++)
{
int endptr = a + (int)lstsqrt;
pointerlist.push_back((&*listOfValues.begin() + endptr)); //( (lstsqrt - 1) + a) );
}
And this is my end() method
int* end() {return (pointerlist.back());}
And this is then passed to my toTry Variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个问题可能是这一行:
您的
listOfValues
是一个std::list
,因此它的值没有存储在连续的内存块中。因此,您将使用listOfValues.begin()
获得第一个元素的迭代器,使用*
取消引用迭代器,使用&< 获取该迭代器的地址/code> 来获取
int*
,然后添加一些值,该值指向内存中的某个位置,但您不知道它是什么。尝试这样做:
将
endptr
添加到迭代器(以使其沿着列表前进),然后取消引用并获取地址。实际上,您可能需要使用advance
而不是 <代码>+。One problem is likely to be this line:
Your
listOfValues
is astd::list
, and therefore its values are not stored in a contiguous block of memory. So you're getting an iterator to the first element withlistOfValues.begin()
, dereferencing the iterator with*
, taking the address of that with&
to get anint*
, then adding some value which points somewhere off into memory that you don't know what it is.Try doing this instead:
where you add
endptr
to the iterator (to advance it along the list), then dereference and take the address. Actually you may need to useadvance
instead of+
.