Can you define the size of an array at runtime in C

后端 未结 10 1258
走了就别回头了
走了就别回头了 2021-01-02 18:35

New to C, thanks a lot for help.

Is it possible to define an array in C without either specifying its size or initializing it.

For example, can I prompt a u

10条回答
  •  天命终不由人
    2021-01-02 19:05

    You can use malloc to allocate memory dynamically (i.e. the size is not known until runtime).

    C is a low level language: you have to manually free up the memory after it's used; if you don't, your program will suffer from memory leaks.

    UPDATE

    Just read your comment on another answer.

    You're asking for an array with a dynamically-changing-size.

    Well, C has no language/syntactic facilities to do that; you either have to implement this yourself or use a library that has already implemented it.

    See this question: Is there an auto-resizing array/dynamic array implementation for C that comes with glibc?

提交回复
热议问题