Search code examples
c++functionpointersroot

How to deal with pointers to define a function? And problem to define a function which returns an array


Suppose I need a function which generates an array r[3] = {x, y, z} where x[1000], y[1000], z[1000] each one is an array and has 1000 double float points. I made a function which returns the location of r form where we need to access the x, y,z by unpacking the memory location. I have the code like:

double cylinder(double radius, double height)
{
    double x[1000], y[1000], z[1000];
    double theta = 0, dtheta = M_PI / 500, dz = height / 1000;
    z[0] = 0;
    y[0] = 0;
    x[0] = radius;
    for (int i = 1; i < 1000; i++)
    {
        z[i] = z[i - 1] + dz;
        theta = theta + dtheta;
        x[i] = radius * cos(theta);
        y[i] = radius * sin(theta);
    }
    double * r[3] = {x,y,z};
    return **r;
}

now if I use

data = cylinder(5, 10);
cout<<data<<endl;

It should return a location but why it returns 5. I need to have the location of 'data' and from that I will get 3 more memory locations which 3 has all the 1000 points on each location. I will be very thankful to get the solution.


Solution

  • This can be easily fixed by using the Standard Library std::vector in conjunction with std::tuple which is like a really lightweight fixed-length array:

    #include <vector>
    #include <tuple>
    
    std::vector<std::tuple<double,double,double>> cylinder(double radius, double height)
    {
        std::vector<std::tuple<double,double,double>> result;
    
        double theta = 0;
        double dtheta = M_PI / 500;
        double dz = height / 1000;
    
        // No need for an array or back-references here
        double x = radius;
        double y = 0;
        double z = 0;
    
        // Add the 0th entry
        result.push_back({ x, y, z });
    
        for (int i = 1; i < 1000; i++)
        {
            z += dz;
            theta = theta + dtheta;
            x = radius * cos(theta);
            y = radius * sin(theta);
    
            // Add subsequent entries
            result.push_back({ x, y, z });
        }
    
        return result;
    }
    

    Where now memory management issues are solved by using containers.

    If x, y and z are semantically important you might even want to make a small struct with those properties instead to give it more context.