c arrays: setting size dynamically?

后端 未结 4 763
余生分开走
余生分开走 2021-01-18 11:25

I am new to C programming. I am trying to set the size of the array using a variable but I am getting an error: Storage size of \'array\' isn\'t constant !!

4条回答
  •  有刺的猬
    2021-01-18 11:54

    To create an array of a non-constant size (i.e. known at compile time), you need to dynamically allocate space for it using malloc() (and correspondingly deallocate it using free() when it is no longer required).

    As others have noted, the ability to declare dynamic arrays is available in C99 compliant compilers.

提交回复
热议问题