Solved

# Return float array with function

Posted on 2004-10-23
399 Views
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

LVL 5

Expert Comment

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

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

OK guys, that made it clear for now. Thank you very mutch.
0

LVL 9

Expert Comment

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

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

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

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

## Featured Post

### Suggested Solutions

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…
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.