gpt4 book ai didi

c - malloc 之前的 (int *) 是什么意思?

转载 作者:太空宇宙 更新时间:2023-11-04 00:40:48 26 4
gpt4 key购买 nike

以下代码中的 (int *) 是做什么的?

int *ptr = (int *) malloc(10 * sizeof (int));

我是 C 的新手,我已经看到上面的代码有和没有 (int *),所以我想知道它的作用。

最佳答案

这意味着“将 void* 指针转换为 int* 指针”——malloc() 返回 void* 并且您要求编译器将 void* 视为 int*。这种围绕 malloc() 的构造仅在 C++ 代码中需要,并且是 totally unneeded and even evil in C因为它可能会导致相当微妙但具有破坏性的错误。

关于c - malloc 之前的 (int *) 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9014650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com