0

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?

user253751
  • 57,427
  • 7
  • 48
  • 90
  • It sounds like VS2015 is using an earlier C standard that doesn't support dynamically sized arrays. You should check their documentation to see if there's an option to support a later C standard. – Tom Karzes Apr 20 '16 at 22:31
  • 1
    see [Variable length arrays C99 not supported in C](https://stackoverflow.com/questions/7871019/variable-length-arrays-c99-not-supported-in-c) and [Which compiler should I trust?](https://stackoverflow.com/questions/18521398/which-compiler-should-i-trust?lq=1) – kaylum Apr 20 '16 at 22:40
  • 1
    MSVC is not compliant to the C standard. And they never supported features introduced with C99 like _variable length arrays_ (VLAs). You should get a modern C compiler which supports standard C like clang or gcc. Note: Code::Blocks is an IDE, not a compiler. – too honest for this site Apr 20 '16 at 22:41
  • @Olaf, Well its kinda implied that it's an IDE, its just sometimes i refer to one as the other. Nevertheless, google is not very helpful at aiding me the information to why MSVC is not supporting C99 standard, and now it got me to question why am i allowed to define a variable within 'for' loop's brackets. Isn't that a feature only supported by C99 standard? C::B doesn't let you use it that way unless u specifically tell it to use C99 standard when compiling. – theViTALiTY Apr 20 '16 at 23:57
  • You missed the point. The IDE is not responsible for compiling the code. So the underlying compiler is what matters. AFAIK you can very well call MSVC from Code::Blocks, too. And maybe you can call gcc or clang from VS. About the "whys and why nots" of MS supporting something: I have learned not to think about it. Tempora mutantur, MS is not the only to provide software ... If in doubt if something is an extension of C or not, read the documentation of the compiler and the **only** authoritative reference, which is ISO9899:2011. There is no other C standard. – too honest for this site Apr 21 '16 at 00:00

0 Answers0