简体   繁体   English

传递此双指针并获取值有什么问题?

[英]What's wrong on passing this double pointer and get the value?

I'm new/noob programmer of C++, and I've this problem. 我是C ++的新手/菜鸟程序员,遇到了这个问题。 I want to pass a pointer of double to a function (which will process some data on it) and read (after the process) a fixed value of that "array". 我想将double指针传递给一个函数(该函数将处理一些数据)并读取(在该过程之后)该“数组”的固定值。 I've do this : 我已经做到

void ReadDoubles(double* samples, int size)
{
    for (int i=0; i < size; ++i)
    {
        *samples = i*10.1;
        samples++;
    }    
}

int main()
{
    int size = 10;
    double *values=0;
    ReadDoubles(values, size);
    cout << *(values+3);
}

BUt of course it seems I can't init the pointer that way. 当然,看来我不能以这种方式初始化指针。 I think I need to init the pointer allocating 10 values? 我想我需要初始化分配10个值的指针吗? Tried: 尝试:

double *values[size];

but that's not the solution. 但这不是解决方案。 How would you do this simple task? 您将如何完成这个简单的任务?

You need to allocate the array at first. 您首先需要分配数组。 Here you are 这个给你

#include <iostream>

void ReadDoubles( double* samples, size_t size )
{
    for ( size_t i = 0; i < size; ++i )
    {
        *samples = i*10.1;
        samples++;
    }    
}

int main()
{
    size_t size = 10;
    double *values = new double[size];
    //               ^^^^^^^^^^^^^^^^  

    ReadDoubles( values, size );

    std::cout << *(values+3) << std::endl;

    delete []values;
}

The program output is 程序输出为

30.3

If you don't want to use the operator new then there are two general approaches. 如果您不想使用new运算符,则有两种通用方法。 Either you can declare an array as for example 您可以声明一个数组,例如

int main()
{
    const size_t size = 10;
    //^^^^
    double values[size];
    //     ^^^^^^^^^^^^  

    ReadDoubles( values, size );

    std::cout << *(values+3) << std::endl;
}

or you can use standard class std::vector<double> .In this case the function should be rewritten appropriately. 或者可以使用标准类std::vector<double> 。在这种情况下,应适当地重写该函数。

For example 例如

#include <iostream>
#include <vector>

void ReadDoubles( std::vector<double> &samples, size_t size )
{
    samples.resize( size );

    for ( size_t i = 0; i < size; ++i )
    {
        samples[i] = i*10.1;
    }    
}

int main()
{
    size_t size = 10;
    std::vector<double> values;

    ReadDoubles( values, size );

    std::cout << values[3] << std::endl;
}

If you are not allowed to change the RealDoubles function and you must have a function return the size then the following should work: 如果不允许您更改RealDoubles函数,并且必须具有返回大小的函数,则应执行以下操作:

#include <string>
#include <iostream>
#include <math.h>
#include <cmath>

using namespace std;

void ReadDoubles(double* samples,int size)
{
    for (int i=0; i < size; ++i) {
        *samples = i*10.1; 
        samples++;
    }
}

int get_size()
{
    return 10;
}

int main()
{
    int size = get_size(); // get size from function
    //double *values=0;
    double *values = new double[size] {0}; // Use new to allocate array. Optional: use {0} to init first element to 0, others default initialized to 0
    ReadDoubles(values,size);
    cout << *(values+3);
    delete[] values;
}

If you prefer to avoid new and delete then you can let a std::vector manage the container for you: 如果您希望避免newdelete那么可以让std::vector为您管理容器:

#include <string>
#include <iostream>
#include <math.h>
#include <cmath>
#include <vector>

using namespace std;

void ReadDoubles(double* samples,int size)
{
    for (int i=0; i < size; ++i) {
        *samples = i*10.1; 
        samples++;
    }
}

int get_size()
{
    return 10;
}

int main()
{
    int size = get_size(); // get size from function
    //double *values=0;
    std::vector<double> value_container(size,0); // vector will do the new and delete for us
    double *values = value_container.data();
    ReadDoubles(values,size);
    cout << *(values+3);
} // vector destructor will do delete when it goes out of scope

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM