I wrote a pretty straight forward code that gives a value of X for 3 called functions within variable arguments function. The problem is, when i compile this within CodeBlocks, it runs perfectly, but VS2015 is giving me errors about using a variable for an array size that is not constant (Line 28), and giving me errors on line 32 that state:
- expected a ')' - Line 28
- C2143 syntax error: missing ')' before '*' - Line 32
- C2059 syntax error: ')' - Line 32
- C2143 syntax error: missing ')' before ';' - Line 32
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
void f(double, int, ...);
int main(int argc, char *argv[])
{
double dx;
printf(" x sin(x) cos(x) exp(x) \n");
printf("===== ========= ========= =========\n");
for (dx = -1; dx < 1; dx += 0.01)
f(dx, 3, sin, cos, exp);
getchar();
getchar();
return 0;
}
void f(double x, int n, ...)
{
va_list arg;
va_start(arg, n);
double rez[n]; // LINE 28
for (int i = 0; i < n; i++)
{
double (*f)(double) = va_arg(arg, double (*)(double)); // LINE 32
rez[i] = (*f)(x);
}
printf("%5.2f ", x);
for (int i = 0; i < n; i++)
printf("%9.5f ", rez[i]);
printf("\n");
free(rez);
va_end(arg);
}
So, what is the difference between the compilers since one lets me compile it like a charm and other one doesn't?