Variable Sized Arrays in C

前端 未结 5 1513
予麋鹿
予麋鹿 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:50

    I'm sorry this is not an answer, but I'd like to point out a potential problem with using variable-length arrays. Most of the code that I have come across looks like this.

    void foo(int n)
    {
        int bar[n];
        .
        .
    }
    

    There is no explicit error checking here. A large n can easily cause problems.

提交回复
热议问题