如何在 C++ 中的动态分配数组中正确插入和显示数据?

发布于 2025-01-11 11:23:38 字数 1174 浏览 1 评论 0原文

我在尝试正确显示正确的内存地址时遇到了麻烦,因此我不知道在哪个内存地址中输入数据。

    #include <iostream>
    
    using namespace std;

    int main() {

       system("cls");

       int *p = new int[2];

       for(int i = 0; i < 2; i++) {
           cout << "Enter value for address " << p << ": ";
           cin >> p[i];
       }

       for(int i = 0; i < 2; i++) {
           cout << *p << " " << p << endl;
           p++;
       }

    }

以下是输入数据时的输出:

在此处输入图像描述

以下是显示它们时的输出:

在此处输入图像描述

我担心的是输入数据时它不会输出正确的内存地址。

输入图片description here

但是当显示它们时,显示正确的内存地址似乎没有问题。

输入图片此处描述

I've been having trouble trying to properly display the correct memory address so I don't know in which memory address I'm inputting data.

    #include <iostream>
    
    using namespace std;

    int main() {

       system("cls");

       int *p = new int[2];

       for(int i = 0; i < 2; i++) {
           cout << "Enter value for address " << p << ": ";
           cin >> p[i];
       }

       for(int i = 0; i < 2; i++) {
           cout << *p << " " << p << endl;
           p++;
       }

    }

Here is the output when inputting data:

enter image description here

Here is the output when displaying them:

enter image description here

My concern is it doesn't output the correct memory address when inputting data.

enter image description here

But when displaying them it seems to have no problem displaying the correct memory address.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

灯下孤影 2025-01-18 11:23:38

您的输入循环在每次迭代时按原样显示 p 指针(即数组的基地址)。它不会在每次迭代时调整指针,这与输出循环不同。

您还泄漏了数组,因为当您使用完它后,您没有delete[]'它。

试试这个:

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {

    system("cls");

    int *p = new int[2];

    for(int i = 0; i < 2; i++) {
        cout << "Enter value for address " << &p[i] << ": ";
        cin >> p[i];
    }

    for(int i = 0; i < 2; i++) {
        cout << p[i] << " " << &p[i] << endl;
    }

    delete[] p;
    return 0;
}

或者:

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {

    system("cls");

    int *p = new int[2];

    int *elem = p;
    for(int i = 0; i < 2; i++) {
        cout << "Enter value for address " << elem << ": ";
        cin >> *elem;
        ++elem;
    }

    elem = p;
    for(int i = 0; i < 2; i++) {
        cout << *elem << " " << elem << endl;
        ++elem;
    }

    delete[] p;
    return 0;
}

Your input loop is displaying the p pointer as-is (ie, the base address of the array) on every iteration. It is not adjusting the pointer on each iteration, unlike your output loop which does.

You are also leaking the array, as you are not delete[]'ing it when you are done using it.

Try this instead:

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {

    system("cls");

    int *p = new int[2];

    for(int i = 0; i < 2; i++) {
        cout << "Enter value for address " << &p[i] << ": ";
        cin >> p[i];
    }

    for(int i = 0; i < 2; i++) {
        cout << p[i] << " " << &p[i] << endl;
    }

    delete[] p;
    return 0;
}

Alternatively:

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {

    system("cls");

    int *p = new int[2];

    int *elem = p;
    for(int i = 0; i < 2; i++) {
        cout << "Enter value for address " << elem << ": ";
        cin >> *elem;
        ++elem;
    }

    elem = p;
    for(int i = 0; i < 2; i++) {
        cout << *elem << " " << elem << endl;
        ++elem;
    }

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