【问题标题】:How do I use pointers with arrays in c++ and return a pointer value?如何在 c++ 中将指针与数组一起使用并返回指针值?
【发布时间】:2023-03-03 05:02:23
【问题描述】:

我试图在以下代码中尽可能使用指针,但很难弄清楚如何准确地建立指针以及如何在我的第一个函数结束时返回指针值。我已经对此主题进行了一些研究,但到目前为止我发现的方法都没有帮助,所以我希望你能有一些专门的提示。

注意:我是初学者。

#include <iostream>
using namespace std;

int mode(int *pies[], int size) {


    int count = 1;
    int max = 0;
    int *mode=pies[0];


    for (int i=0; i<size-1; i++)
    {
        if (pies[i] == pies[i+1])
        {
            count++;
            if (count>max)
            {
                max = count;
                mode = pies[i];
            
            }
    
        }
        else
            count = 1;
    
    }

    return *mode;
}


int main() {

    int n;

    cout<<"Input the number of people: "<<endl;

    cin>>n;

    int survey[n];

    cout << "Enter the amount of pie eaten by each person:" << endl;


    for(int i = 0; i < n; i++) {
    
        cout <<"Person "<<(i + 1)<< ": "<<endl;
    
        cin>>survey[i];
    
    }

    cout<<"Mode: "<<mode(survey, n)<< endl;

        return 0;


}

【问题讨论】:

  • 尽可能使用指针 为什么? :/ 无论如何,如果你想返回一个指针,那么就以你返回任何其他数据类型的方式返回一个指针。当然,您还必须将函数的返回类型更改为指针。
  • mode 接受int** 参数,您传递的是int*。
  • 只要使用--int模式(int* pies, int size)
  • 使用std::vector&lt;int&gt;。它是 C++,而不是 C。
  • 方便阅读:What is array to pointer decay? TL;DR 版本:数组不是指针,而是根据需要隐式转换为指针。结果,您几乎从不传递数组,因为您悄悄地传递了指向数组的指针。这种行为可能很烦人,但在过去没有资源按值传递数组时,这种行为非常有意义。

标签: c++ pointers return


【解决方案1】:

这是一个尝试回答。 在您的main() 中,您使用mode(survey, n) 调用mode() 函数,而int survey[n]; 是int 的数组,因此您可以使用int mode(int *pies, int size) 而不是int mode(int *pies[], int size)(因为数组int survey[n] 可以是隐式转换为指针)。

但是,您需要在函数中修改另外两件事:

  • int *mode=pies[0]; 是错误的,因为 pies[0] 是 int 数组的第一个元素,因此是 int,而 int* mode 是不兼容的 int 上的指针。 mode 应该是int 以接收pies[0]。那么正确的代码是int mode = pies[0]。
  • 您的函数签名是int mode(int *pies, int size),因此,您应该再次返回int。然后你应该只是return mode;

这些只是关于如何编译代码的提示。 您的下一步是正式确定您希望它做什么,然后相应地修改代码

注意:正确的做法是先想好你想实现什么,然后再编码(但让我们说这是为了互相帮助)

要开始使用指针,你可以先看一些简单的教程:

这是经过上述修改的修改后的代码(它可以编译):

#include <iostream>
using namespace std;

int mode(int *pies, int size) {


    int count = 1;
    int max = 0;
    int mode=pies[0];


    for (int i=0; i<size-1; i++)
    {
        if (pies[i] == pies[i+1])
        {
            count++;
            if (count>max)
            {
                max = count;
                mode = pies[i];

            }

        }
        else
            count = 1;

    }

    return mode;
}


int main() {

    int n;

    cout<<"Input the number of people: "<<endl;

    cin>>n;

    int survey[n];

    cout << "Enter the amount of pie eaten by each person:" << endl;


    for(int i = 0; i < n; i++) {

        cout <<"Person "<<(i + 1)<< ": "<<endl;

        cin>>survey[i];

    }

    cout<<"Mode: "<<mode(survey, n)<< endl;

        return 0;


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2021-06-25
    • 1970-01-01
    相关资源
    最近更新 更多