我应该明确地转换malloc()函数的返回值? [英] Should I explicitly cast malloc()'s return value?

查看:114
本文介绍了我应该明确地转换malloc()函数的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想请教一下以下的情况:

 的char *温度;
TEMP =的malloc(10);

由于的malloc的返回类型无效* ,将通过的malloc 隐式转换为的char * 之前被分配到临时类型?是什么标准说,在这方面?

如果我们的指针变量是一些结构类型,例如:

 结构节点*温度;
TEMP =(结构节点*)malloc的(的sizeof(结构节点));

如果我们没有它转换为结构节点* 类型分配内存来临时,将它隐式转换为结构节点* 类型或者是有必要明确地将它转换为结构节点* 键入<?/ p>

解决方案

在C A void指针可以被分配到任何指针没有明确的转换。

I wanted to ask about the following case:

char *temp;
temp = malloc(10);

Since the return type of malloc is void*, will the pointer returned by the malloc be implicitly cast to char* type before being assigned to temp? What does the standard say in this regard?

If our pointer variable is some struct type for example:

struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));

If we allocate memory to temp without casting it to struct node* type, will it be implicitly cast to struct node* type or is it necessary to explicitly cast it to struct node* type?

解决方案

A void pointer in C can be assigned to any pointer without an explicit cast.

这篇关于我应该明确地转换malloc()函数的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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