c语言中关于malloc函数的问题

p1 = p2 = (struct student*)malloc(LEN);
等价于
p1 = (struct student*)malloc(LEN);
p2 = (struct student*)malloc(LEN);

吗?

这是不等价的,楼主可以通过比较地址来判断。

第一条是开辟出一段空间,p1与p2指向相同。

后面两条是开辟两段空间,p1与p2分别指向。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-03-15
p1 = p2 = (struct student*)malloc(LEN);等价于:
p2=(struct student*)malloc(LEN);
p1=p2;
仅分配了一块空间;

p1 = (struct student*)malloc(LEN);
p2 = (struct student*)malloc(LEN);
是分配了两块空间,p1、p2指向不同空间。
相似回答