C: Expanding an array with malloc

后端 未结 4 980
逝去的感伤
逝去的感伤 2020-12-08 07:53

I\'m a bit new to malloc and C in general. I wanted to know how I can, if needed, extend the size of an otherwise fixed-size array with malloc.

Example:



        
4条回答
  •  有刺的猬
    2020-12-08 08:32

    a) you did not use malloc to create it so you cannot expand with malloc. Do:

      mystruct *myarray = (mystruct*)malloc(sizeof( mystruct) *SIZE);
    

    b) use realloc (RTM) to make it bigger

提交回复
热议问题