gpt4 book ai didi

calloc() 、类型转换和指针

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

我是 C 的新手。

我知道这是正确的:

char* Str;
Str = (char*)calloc(Str_Len, sizeof(char));

,但为什么这是不正确的?

char* Str;
*Str = (char*)calloc(Str_Len, sizeof(char));

如何修改呢?谢谢。

最佳答案

首先是合法的,但是do not cast the return value of malloc or calloc在 C 中(因为它们的返回类型是 void *)。
在第二种情况下 Strchar 类型,你不能为它分配超过 1 字节的内存。 calloc 返回 pointer*Strchar 类型。您不能将 char * 数据类型分配给 char 类型。

关于calloc() 、类型转换和指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20957303/

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