错误:分配给从类型'无效*'类型'值'时,不兼容的类型? [英] Error: incompatible types when assigning to type 'values' from type 'void *'?

查看:460
本文介绍了错误:分配给从类型'无效*'类型'值'时,不兼容的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的时候我尝试malloc的一些内存为我的int型结构此错误。

  typedef结构_values​​ {
随机整数;
}值;

我曾尝试下面的行,但我的编译器不喜欢它。如何解决这个错误吗?

  v值;
V =的malloc(sizeof的(值));


解决方案

您忘了加上值后面的星号(*) v 将其标记为一个指针:值* v;

现在设置它的方式,在V(不带星号)被定义为一个堆栈变量和一次函数结束将在堆栈上被分配并丢弃。它的类型将是简单
的malloc 用于在堆中分配内存,并返回一个指针内存。正弦函数无法知道它返回它作为一个无效* 类型类型的方式 - 它给你的错误 - 你试图分配一个无效* 键入成结构类型,编译器不能做的,也不是编译器能找到,可以解决该问题的合法投

I get this error when I'm trying to malloc some memory for my struct of ints.

typedef struct _values {
random ints;
} values;

I have tried the lines below but my compiler doesn't like it. How do I fix the error?

values v;
v = malloc(sizeof(values));

解决方案

You forgot to add the asterisk (*) after the values and before the v to mark it as a pointer: values *v;

The way you set it now, the v (without the asterisk) is defined as a stack variable and would be allocated on the stack and discarded once the function ends. It's type will be simply values. malloc is used to allocate memory on the heap and returns a pointer to the memory. Sine the function has no way of knowing the type it returns it as a void * type - Which gives you your error - you're attempting to assign a void * type into a struct type, which the compiler can't do, nor can the compiler find a legitimate cast that could resolve the problem.

这篇关于错误:分配给从类型'无效*'类型'值'时,不兼容的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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