Variable Sized Arrays vs calloc in C

前端 未结 5 1971
挽巷
挽巷 2020-12-03 06:37

On the discussion of dynamic memory here: \"Intro to C Pointers and Dynamic Memory\"

The author states:

A memory block like this can effective

5条回答
  •  执笔经年
    2020-12-03 06:53

    Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++. So, don't foget to set compiler flag -std=c99 or -std=gnu99. Following example will work

    #include
    
    int main()
    {
        int n;
        printf("\n\tEnter the number of digits: ");
        scanf("%d", &n);
    
        char str[n];
        for(int i=0; i < n; i++) {
            scanf("%s", &str[i]);
        }
    
        printf("\n\tThe entered digits are: %s", str);
    return 0;
    }
    

    I garantee that :-)

提交回复
热议问题