Memory allocation for char array

后端 未结 5 1397
礼貌的吻别
礼貌的吻别 2021-02-03 14:46

I have a big problem with C language when it comes to strings, char * \'s or whatever... So in this particular case I have a huge problem. I want to create an array

5条回答
  •  萌比男神i
    2021-02-03 14:46

    How can I announce the array and then define it's size?

    Don't; 'announce' it when you know what size it needs to be. You can't use it before then anyway.

    In C99 and later, you can define variables when needed — anywhere in a statement block. You can also use VLAs (variable-length arrays) where the size is not known until runtime. Don't create enormous arrays as VLAs (e.g. 1 MiB or more — but tune the limit to suit your machine and prejudices); use dynamic memory allocation after all.

    If you're stuck with the archaic C89/C90 standard, then you can only define variables at the start of a block, and arrays have sizes known at compile time, so you have to use dynamic memory allocation — malloc(), free() etc.

提交回复
热议问题