• C

Return float array with function

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();

}

MrCWizardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

van_dyCommented:
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
x-panderCommented:
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
MrCWizardAuthor Commented:
OK guys, that made it clear for now. Thank you very mutch.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

jhshuklaCommented:
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
stefan73Commented:
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
PaulCaswellCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stefan73Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.