与C ++中的结构初始化等效的C编程语言

时间:2019-10-13 21:10:21

标签: c++ c struct

假设列表是某种结构

list *temp;
temp = new list;

上面是c ++,在C中是否等效?

1 个答案:

答案 0 :(得分:5)

list *temp;
temp = malloc(sizeof(list));

[...]

free(temp);

C没有new关键字-您必须分配和释放要手动使用的内存。 new还会做一些work behind the scenes,例如调用构造函数并返回正确的指针类型-malloc不能解决这个问题,必须由程序员处理。