How to return(return) array of function with a variable number of parameters?

Here is the assignment:
ed66abd2abc74f3aa25aad64d22fe847.png

What is my mistake?
C (Si)
#include <stdio.h>
#include <stdlib.h>

#define END_PARAM -1

int max(double a, ...)
{
 double *p = &a; /* pointer to option value */
 double *str = (double*)malloc(30 * sizeof(double));

 while (*p != END_PARAM)
{
 *str = *p; /* a writable array */

 str++; /*go to next index in array*/
 p++; /* go to sledujushem parameter */
}

 /* return result */
 return *str;
}

void main()
{
 printf("result = %d\n\n", max(100.5, 25.5, 30.76, 9.1, 22.45, END_PARAM));

system("pause");
}</stdlib.h></stdio.h>
June 27th 19 at 14:52
3 answers
June 27th 19 at 14:54
First mistake - you need to return not an int, a double*.
The second conclusion in printf - again print int(%d) is wrong. Format to output array of doubles no, as far as I know, it is necessary to deduce in a cycle.
The third memory allocation is incorrect. Where did the 30 * sizeof(double)? You have to pass in the parameters, first the size, then copy. Can be copied by using memcpy, by the way, not a loop.
And walk as the list of parameters seems to be wrong, I don't know.
And walk as the list of parameters seems to be wrong, I don't know.

In C++ exactly wrong, gcc often generates parameter passing via registers and not via stack, which may be a pointer to the register? And of course, the standard does not stipulate that the transfer must be through the stack. - Zit commented on June 27th 19 at 14:57
: that's why I wrote I don't know it's implementation details that I somehow started to forget. No longer work with si. - Art_Christians commented on June 27th 19 at 15:00
June 27th 19 at 14:56
Horror. Return the result of dereferencing a double* in an int function is strong.
Apparently expect from you in General terms about the following:
double *const getArray() {
 double *const result = new double[10];
 // Fill the array as you wish
 return result;
}
Oh, that's right, out of habit I forgot to change the type.
Still a little do not understand your decision.
I assumed that the function with variable number of parameters serves some value, then they need to rewrite it in a dynamic array and return the result to returnon. Back or I not so understood? - Zit commented on June 27th 19 at 14:59
: What's your return one value?
You need to return a pointer to an array - Art_Christians commented on June 27th 19 at 15:02
June 27th 19 at 14:58
You are walking on memory in the hope that parameters are passed via the stack, but it may not be the case and quite often the compiler passes them through the registers. There are special tools, Googly va_list

Find more questions by tags C++