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

后端 未结 10 1282
走了就别回头了
走了就别回头了 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 18:49

    Above given answers are correct but there is one correction, the function malloc() reserve a block of memory of specified size and return a pointer of type void* which can be casted into pointer of any form. Syntax: ptr = (cast-type*) malloc(byte-size)

    #include
    #include
    int main(int argc,char* argv[]){
    int *arraySize,length;
    scanf("%d",&length);
    arraySize = (int*)malloc(length*sizeof(int));
    for(int i=0;i

提交回复
热议问题