为什么我们需要投什么malloc的回报? [英] Why do we need to cast what malloc returns?
本文介绍了为什么我们需要投什么malloc的回报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int length = strlen(src);
char *structSpace = malloc(sizeof(String) + length + 1);
String *string = (String*) structSpace;
int *string = (int*) structSpace;
*我创建了一个名为字符串结构
*I created a struct called String
推荐答案
您没有。 无效*
将隐式转换,无论你在C.见需要还的为什么你会想要明确地避免铸造的malloc在C.回报@思南的答案C FAQ 进一步说明了为什么这已经不一致遵循。
You don't. void*
will implicitly cast to whatever you need in C. See also the C FAQ on why you would want to explicitly avoid casting malloc's return in C. @Sinan's answer further illustrates why this has been followed inconsistently.
这篇关于为什么我们需要投什么malloc的回报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文