打印 int 指针的值

发布于 2025-01-04 16:59:31 字数 1260 浏览 0 评论 0原文

我一直在努力解决这个问题,但我似乎无法让它正常工作。我正在返回指针列表的最后一个值,并且我想打印它,但它正在打印一个非常随机的数字。我假设这是指针的内存地址,但是当我取消引用它时,我的输出仍然做同样的事情。

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气 2025-01-11 16:59:31

一个问题可能是这一行:

pointerlist.push_back((&*listOfValues.begin() + endptr));

您的 listOfValues 是一个 std::list,因此它的值没有存储在连续的内存块中。因此,您将使用 listOfValues.begin() 获得第一个元素的迭代器,使用 * 取消引用迭代器,使用 &< 获取该迭代器的地址/code> 来获取 int*,然后添加一些值,该值指向内存中的某个位置,但您不知道它是什么。

尝试这样做:

pointerlist.push_back((&*(listOfValues.begin() + endptr)));

endptr 添加到迭代器(以使其沿着列表前进),然后取消引用并获取地址。实际上,您可能需要使用 advance 而不是 <代码>+。

One problem is likely to be this line:

pointerlist.push_back((&*listOfValues.begin() + endptr));

Your listOfValues is a std::list, and therefore its values are not stored in a contiguous block of memory. So you're getting an iterator to the first element with listOfValues.begin(), dereferencing the iterator with *, taking the address of that with & to get an int*, then adding some value which points somewhere off into memory that you don't know what it is.

Try doing this instead:

pointerlist.push_back((&*(listOfValues.begin() + endptr)));

where you add endptr to the iterator (to advance it along the list), then dereference and take the address. Actually you may need to use advance instead of +.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文