realloc structure with function in C

六月ゝ 毕业季﹏ 提交于 2019-12-02 08:37:24

realloc() may return a different pointer than the original one but you assign that to temp only so the calling function still works with the original pointer afterwards. Change double_array_size() to return the new pointer returned by realloc() and call

d = double_array_size(d, array_size);

Furthermore you should always check the result fo malloc(), realloc() etc. They may return NULL if there is no more memory available

Combining Ingo's and codroipo's answers, you have to either return the new pointer from double_array_size, or you have to pass in a pointer to d so you can update the pointer from double_array_size

Realloc reallocates memory, so probably memory pointed by d will be released, so double_array_size has to edit d, you could try:

void double_array_size(struct course** d, int new_size){
*d = realloc(*d, new_size * sizeof(struct course));
.
.
.
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!