0

My question is related to this thread.

Here is the code

#include <stdio.h>

int main(int argc, char *argv[printf("Hello, world!\n")]) {}

I accidently saved it as a *.cpp file and tried to compile it with g++. But I got an error and a warning.

error: expected ',' or '...' before 'argv'
warning: second argument of 'int main(int, char*)' should be 'char ** '

I know the above code is not Standard C++ [size of an array must be a constant expression in C++] but I always thought g++ supports Varible Length Array as an extension. Where am I wrong?

P.S : The above code gets compiled with CLang++

C:\Users\SUPER USER\Desktop>type check.cpp
#include <stdio.h>

int main(int argc, char *argv[printf("Hello, world!\n")]) {}
C:\Users\SUPER USER\Desktop>clang++ check.cpp

C:\Users\SUPER USER\Desktop>
Community
  • 1
  • 1
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345

1 Answers1

5

g++ allows (again, as an extension) VLAs. I think it just doesn't allow them in parameter lists. This compiles in g++ 4.4.1.

#include <stdio.h>

int main(int argc, char *argv[])
{
    char *array[printf("Hello, world!\n")];
}
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539