0

is there an easy way to compile C code in visual studio using gnu11? I wrote the code on Xcode and it works perfectly fine, but doesn't even compile on visual studio, the main problem is that I defined an integer named "size", which takes the length of a string and then defined an array with the size "size". works perfectly normal in Xcode, can't even compile in visual studio.

int size = strlen(input);

int sorted_input[size];

  • The Microsoft compiler doesn't support VLAs (variable length arrays). Replace `sorted_input[size]` with `sorted_input[1000]` and it should compile. – Jabberwocky Jan 14 '21 at 14:14
  • Otherwise maybe [this](https://stackoverflow.com/questions/14768073/how-to-use-gcc-with-microsoft-visual-studio) helps – Jabberwocky Jan 14 '21 at 14:18
  • There's always `malloc` or `calloc`. – Retired Ninja Jan 14 '21 at 14:22
  • @Jabberwocky I did put the size 1000 and now its crashing because its accessing out of range array, I'll try using the malloc. thanks for both of you. – Basel Massarweh Jan 14 '21 at 14:31
  • @BaselMassarweh my suggestion was for being it able to be compiled, of course if `strlen(input)` is larger than 1000 you'll have a problem. If you use `malloc` don't forget to free the allocated memory. Otherwise you still have [`_alloca`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/alloca?view=msvc-160) which does basically the exact same thing as VLAs: `int *sorted_input = _alloca(size * sizeof(*sorted));` – Jabberwocky Jan 14 '21 at 14:33

0 Answers0