Variable Sized Arrays in C

前端 未结 5 1520
予麋鹿
予麋鹿 2020-12-17 02:17

I guess my question is whether the following is valid C

int main(void) {
  int r = 3;
  int k[r];
  return 0;
}

If so, would some one care

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-17 02:55

    It is a GCC extension that the current MSVC does not support. You can replace it in MSVC fairly easily with an _alloca (a stack allocation that requires no manual deallocation on the part of the programmer):

    #include 
    
    ...
    
    int *k = (int *)_alloca(sizeof(*k)*r);
    

提交回复
热议问题