Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Return float array with function

Posted on 2004-10-23
9
Medium Priority
?
445 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
Comment
Question by:MrCWizard
9 Comments
 
LVL 5

Expert Comment

by:van_dy
ID: 12389958
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
ID: 12389982
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
ID: 12390162
OK guys, that made it clear for now. Thank you very mutch.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:jhshukla
ID: 12397173
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
ID: 12399347
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:
PaulCaswell earned 80 total points
ID: 12400286
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
ID: 12401297
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

916 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