Dynamic array in C — Is my understanding of malloc and realloc correct?

前端 未结 3 1514
梦如初夏
梦如初夏 2020-12-12 17:25

I am learning how to create dynamic 1D arrays in C. The code below tries to do the following:

  1. Using malloc, create a dynamic array of length
3条回答
  •  轮回少年
    2020-12-12 18:03

    In C, you should not cast the return value of malloc().

    Also, it's a bad idea to encode the type in the malloc() argument. This is a better way:

    double* data = malloc(10 * sizeof *data);
    

提交回复
热议问题