Is it possible to allow a user to enter an array size with a keyboard?

后端 未结 5 1729
萌比男神i
萌比男神i 2021-01-13 10:21

Is it possible to allow the user to enter the size of an array with a keyboard?

I know that arrays can\'t change size. The only solution I could think of is this:

5条回答
  •  攒了一身酷
    2021-01-13 11:14

    No, you can't have dynamic allocations like that on the stack (yes, I know, some compilers have extensions for this, but generally speaking the answer is no). You'd want to use malloc or new to dynamically allocate room on the heap. Or of course there are always structures to make life easier, like vector.

提交回复
热议问题