1

As title states the code compiles and outputs in the GCC just fine, I want to step by step follow the code in visual so I can make sure I fully understand how things are working but it throws "expression must have a constant value" for the int even[len_sub_arr]; int

odd[len_sub_arr]; lines.

   #include <stdio.h>

void print_arr(int len, int arr[], char name[]) {
  //print number array
  printf("Listing of %s below: \n", name);
  for(int i=0; i < len; i++) {
    printf("%s[%d] = %d\n", name, i, arr[i]);
  }
  printf("\n");
}

int main(void) {
  //initialize and print lengths
  int arr_nums[] = {12, 75, 23, 43, 94, 82, 37, 6, 70, 59};
  int len_arr = sizeof(arr_nums) / sizeof(int);
  printf("Length of arr_nums is %d\n", len_arr);

  int len_sub_arr = len_arr / 2;
  **int even[len_sub_arr];
  int odd[len_sub_arr];**
  printf("Therefore, the length of the even and odd arrays will be = %d\n\n", len_sub_arr);

  //print original array
  print_arr(len_arr, arr_nums, "arr_nums");

  //keep track of even and odd indices
  int even_idx = 0;
  int odd_idx = 0;
  //sort original data into even and odd arrays
  for(int i=0; i< len_arr; i++) {
    if(arr_nums[i] % 2 == 0) {
      even[even_idx] = arr_nums[i];
      even_idx++;
    }
    else {
      odd[odd_idx] = arr_nums[i];
      odd_idx++;
    }
  }

  //print even and odd arrays
  print_arr(len_sub_arr, even, "even");
  print_arr(len_sub_arr, odd, "odd");

  return 0;
}

I keep running into the discrepancies between GCC and visual studios seems to cause me nothing but problems when I feel like something is about to click for me... should I as a beginner continue using the visual studios suite or is there something else you all would recommend to me to use?

Scott
  • 43
  • 3
  • the **'s are not being used as pointers they are to just highlight the two lines with the errors – Scott Apr 27 '22 at 22:00
  • is gcc and visual studio using the same version of the standard? – thurizas Apr 27 '22 at 22:08
  • 4
    The MS compiler does not support VLAs (variable length arrays). See duplicate for more details. – kaylum Apr 27 '22 at 22:12
  • @Scott to highlight just post a comment on that line, that way people can still copy and compile the code directly – phuclv Apr 28 '22 at 01:29
  • 1
    @kaylum: I don't think this is a duplicate of that question, as this is not a variable length array. Unlike the other question, the length here is determined at compile time, and, in fact, the MS C++ compiler will do so (with proper application of const) but the C compiler doesn't seem to allow const variables to be used as manifest constants even when the values can be determined at compile time. – Adrian McCarthy Apr 28 '22 at 06:19
  • @AdrianMcCarthy it is a variable-length array because `len_sub_arr` is not an integral constant expression. "Determined at compile time" is irrelevant, the standard is not worded in such terms. The standard talks about constant expressions, and `len_sub_arr` as written isn't one. – n. m. could be an AI Apr 29 '22 at 10:05
  • @n. 18.e9-where's-my-share m.: The solutions given in the other question (dynamic allocation) are overkill in this case. All that has to be done here is to rewrite the array dimensions in a way that the C compiler understands it's a constant expression. Changing `len_array` and `len_sub_arr` from variables to preprocessor macros solves the problem without VLAs or dynamic allocation. Given that the solution to this problem is different than the solution to the other, I think it's a disservice to label this question as a duplicate. – Adrian McCarthy Apr 30 '22 at 16:25
  • @AdrianMcCarthy You are right, the duplicate answers are not ideal, but they contain enough information to understand the problem of using VLAs in C. Given that information, one can come up with a suitable solution. – n. m. could be an AI Apr 30 '22 at 21:24
  • probably better dupes: [Can a const variable be used to declare the size of an array in C?](https://stackoverflow.com/q/18848537/995714), [expression must have a constant value](https://stackoverflow.com/q/9219712/995714), [Why in C a const object is not a compile-time constant expression?](https://stackoverflow.com/q/40062767/995714), [In C, why can't a const variable be used as an array size initializer?](https://stackoverflow.com/q/44267827/995714) (in the OP's code it's not `const` so it's even worse). In short, in C a **compile-time constant** is required if VLA is not supported – phuclv May 01 '22 at 02:01
  • I disagree that the alleged duplicate provides enough information to help someone in this circumstance (where the size is known at compile time) and would instead guide people to use dynamic memory when it's unnecessary. The first of @phuclv's links would make a much better choice of duplicate. – Adrian McCarthy May 03 '22 at 07:05

0 Answers0