malloc 中是否需要类型转换? [英] Is typecast required in malloc?

查看:23
本文介绍了malloc 中是否需要类型转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 malloc 中 typecast 有什么用?如果我不在 malloc 中编写类型转换,那么它将返回什么?(为什么在 malloc 中需要类型转换?)

What is the use of typecast in malloc? If I don't write the typecast in malloc then what will it return? (Why is typecasting required in malloc?)

推荐答案

我猜你的意思是这样的:

I assume you mean something like this:

int *iptr = (int*)malloc(/* something */);

在 C 中,您不必(也不应该)从 malloc 转换返回指针.它是一个 void * 并且在 C 中,它被隐式转换为另一种指针类型.

And in C, you do not have to (and should not) cast the return pointer from malloc. It's a void * and in C, it is implicitly converted to another pointer type.

int *iptr = malloc(/* something */);

是首选形式.

这不适用于 C++,它不共享相同的 void * 隐式转换行为.

This does not apply to C++, which does not share the same void * implicit cast behavior.

这篇关于malloc 中是否需要类型转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆