Solved

Return float array with function

Posted on 2004-10-23
399 Views
Last Modified: 2011-10-03
I'm writing an program and one of the functions I'm writing have to return a float array but I'm having a heck of a time figuring out how to put this work.

Can you help me?


Here's the example:

float *GetPoints()
{
      float p[3];

      scanf("%f %f %f", p[0], p[1], p[2]);

      return p;
}


void main(void)
{
      float point[3];


      point = GetPoints();

}

0
Question by:MrCWizard
    7 Comments
     
    LVL 5

    Expert Comment

    by:van_dy
    try this,

    void GetPoints(p)
          float *p;
    {

          scanf("%f %f %f", p, p + 1, p + 2);
    }


    int main(void)
    {
          float point[3];
          GetPoints(point);
          printf("%f %f %f\n", point[0], point[1], point[2]);
          return 0;
    }
    0
     
    LVL 1

    Expert Comment

    by:x-pander
    what you are doing in GetPoints function is returning a pointer to a local variable allocated on stack
    so when function finishes the memory pointed by return value is garbage

    you should do it that way:

    void GetPoints(float p[3])
    {
          scanf("%f %f %f", p[0], p[1], p[2]);
    }


    void main(void)
    {
          float point[3];

          GetPoints(point);
    }
    0
     

    Author Comment

    by:MrCWizard
    OK guys, that made it clear for now. Thank you very mutch.
    0
     
    LVL 9

    Expert Comment

    by:jhshukla
    the solution works but the reason for failure is different. you cannot assign a value to an array. in C/C++ pointers are no different than arrays except that arrays point to a fixed location. once you create an array you cannot assign it a new value. "point = GetPoints()" does exactly that and your code won't compile.
    0
     
    LVL 12

    Expert Comment

    by:stefan73
    Hi x-pander,
    >       scanf("%f %f %f", p[0], p[1], p[2]);
    Careful:
    scanf("%f %f %f", &p[0], &p[1], &p[2]);

    And also be aware that there is no guarantee someon using your function really passes three floats. Better wrap them up, and pass heap memory, and use doubles:

    double *GetPoints()
    {
          double* p;

          p=(double*)malloc(3*sizeof(double));

          scanf("%f %f %f", &p[0], &p[1], &p[2]);

          return p;
    }

    Just make sure you call free() later.



    Cheers!

    Stefan
    0
     
    LVL 16

    Accepted Solution

    by:
    Stefan is right to suggest wrapping it up for security. The best security can be achieved using a struct:

    typedef struct ThreePoints
    {
     double points[3];
    } ThreePoints;

    ThreePoints GetPoints()
    {
          ThreePoints p;

          scanf("%f %f %f", &p.points[0], &p.points[1], &p.points[2]);

          return p;
    }

    Paul
    0
     
    LVL 12

    Expert Comment

    by:stefan73
    Yes, it's normally not "just three points", but they represent something (e.g., 3D coordinates, the size of each of the 3 stooges in inches,... etc.)

    Wrap'em, give them a nice & descriptive name, and use them accordingly.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now