I am trying to write a function with a variable number of parameters without using additional libraries. A function of this type: sum (int number of elements, element1, element2, etc.) For an hour, I have been trying to force my function to count float numbers, but in vain. What am I doing wrong?

#include <iostream> #include <conio.h> float sum (int n, float,...) { int *p = &n; float s = 1; for (int i = 1; i <= n; i++) s*= *(++p); return s; } void main() { std::cout << sum(3,2.2,2.1,2.5); getch(); } 

At the output I get float numbers in huge degrees.

  • I wonder why the multiplication function is called sum ? .. - Harry

2 answers 2

You do everything correctly, did not take into account only one thing: real constants by default are of type double (8 bytes), and not float (4 bytes), because of which values ​​are not read correctly from the stack (more precisely, it is not what is expected there). Here is the correct call and a slightly corrected version:

 #include <iostream> #include <conio.h> float sum (int n, float,...) { float *p = (float *)&n; float s = 1; while (n--) s *= *++p; return s; } int main() { std::cout << sum(3,2.2f,2.1f,2.5f); getch(); return 0; } 

Run and get:

 -8.855 

That just seems right. As a result, we collect one of the two answers - and the method has the right to exist (for educational purposes), and @AnT is right about double. We get this option:

 #include <iostream> #include <conio.h> double sum (int n, double,...) { double *p = (double *)(&n+1); double s = 1; while (n--) s *= *p++; return s; } int main() { std::cout << sum(3,2.2,2.1,2.5); getch(); return 0; } 

And the correct conclusion is 11.55

  • We start. We get 0 . And what is the right thing? In principle, this cannot be "correct", because, as I have already said, float values ​​are never passed as variadic parameters. The fact that you specified the suffix f in the arguments of the case does not change - all the same, double parameters are passed. - AnT
  • @AnT, what do you run? Updated the answer. - paulgri
  • 2
    This is absolutely wrong, since the correct path is described in the standard - va_list and the corresponding macros. - Harry
  • What to do if the teacher requires such a decision based on his knowledge of how parameters are put on the stack? Maybe he wants students to know what is hiding inside? As for practical programming, I absolutely agree that it is necessary to use standard tools. The answer is updated. - paulgri
  • What to do? Well, it seems to me that then write at least that way: right - that’s the only way. For an idiot who demands to do wrong - do it like this, just forget this method immediately after submitting this code! ... - Harry

First, access to the variadic parameters in C and C ++ is shared via the macros of the group va_list/va_start/va_arg . This access cannot be done in any other way. Where did you get this access through int *p = &n; - I'm not clear.

Secondly, when passing float values ​​via variadic parameters, they are always implicitly converted to double values. Therefore, there can be no float inside a function here.

Thirdly, values ​​of the form 2.2 , 2.5 , etc. - this is already double , that is, you don’t even try to transmit any float .

Therefore, access is done here only and precisely through va_arg(..., double) .

  • In the high school textbook, there was the following code for the sum of int elements: #include <iostream> int sum (int n, ...) { int *p = &n; int s = 0; for (int i = 1; i <= n; i++) s+= *(++p); return s; } void main() { std::cout << sum(6,4,5,1,2,3,0); } #include <iostream> int sum (int n, ...) { int *p = &n; int s = 0; for (int i = 1; i <= n; i++) s+= *(++p); return s; } void main() { std::cout << sum(6,4,5,1,2,3,0); } #include <iostream> int sum (int n, ...) { int *p = &n; int s = 0; for (int i = 1; i <= n; i++) s+= *(++p); return s; } void main() { std::cout << sum(6,4,5,1,2,3,0); } , and the condition is 2. Write a function with a variable number of parameters that finds the product of float numbers. - user197085
  • 2
    @John Doe: You can safely throw such a training manual into the furnace. void main ... (sigh ...) - AnT
  • Well, what to do, as taught as taught, hope for himself in the future only. Today, the delivery of the last laboratory - this, 6 am on the clock, I still do. After 3 hours to find out that this is unreal - great. - user197085
  • This is not the Lviv textbook in which the standard of the language is replaced by references to Borland C ++ for DOS? ... - Harry