简体   繁体   English

错误:从“void *”类型分配类型“值”时出现不兼容的类型?

[英]Error: incompatible types when assigning to type 'values' from type 'void *'?

I get this error when I'm trying to malloc some memory for my struct of ints. 当我试图为我的int结构malloc一些内存时,我得到了这个错误。

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; 您忘记在valuesv之前添加星号(*)以将其标记为指针: 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. 现在你设置它的方式,v(没有星号)被定义为一个堆栈变量,并且会在堆栈上分配并在函数结束后被丢弃。 It's type will be simply values . 它的类型只是values malloc is used to allocate memory on the heap and returns a pointer to the memory. malloc用于在堆上分配内存并返回指向内存的指针。 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. 正则函数无法知道它返回的类型为void *类型 - 这会给你错误 - 你试图将一个void *类型分配给一个struct类型,编译器不能这样做,也不能编译器找到一个可以解决问题的合法演员。

You don't need to malloc memory in this case; 在这种情况下你不需要malloc内存; the line values v; 线values v; already allocated the memory ("on the stack"). 已经分配了内存(“在堆栈上”)。 It will automatically be freed when you leave the current scope. 离开当前范围时,它将自动释放。

If you really wanted to allocate the memory "on the heap", so that it persists past the current scope, you need v to be a pointer; 如果你真的想在“堆上”分配内存,那么它会持续超过当前作用域,你需要v作为指针; that is: 那是:

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

Remember to free it when you're finished. 记得在你完成后free它。

values v;

this is an instance of the structure that's allocated on the stack. 这是在堆栈上分配的结构的实例。

v = malloc(sizeof(values));

malloc returns a void* pointer and compiler won't allow you to assign a pointer to an instance. malloc返回一个void*指针,编译器不允许您为实例分配指针。

you need to declare a pointer and then assign the malloc return pointer to it. 你需要声明一个指针,然后为它指定malloc返回指针。

something of the sort 某种东西

values *v = NULL; 值* v = NULL;

v = malloc(sizeof(values));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 test.c:51:4:错误:从类型&#39;void *&#39;分配给类型&#39;blk时,类型不兼容 - test.c:51:4: error: incompatible types when assigning to type ‘blk from type ‘void *’ 错误:分配给类型 'BusRoute' {aka 'struct 时的类型不兼容<anonymous> '} 来自类型 'void *'</anonymous> - error: incompatible types when assigning to type ‘BusRoute’ {aka ‘struct <anonymous>’} from type ‘void *’ 编译器错误:在malloc期间从类型&#39;void *&#39;分配给&#39;struct&#39;时出现不兼容的类型 - Compiler error: incompatible types when assigning to 'struct' from type 'void *' during malloc 从C中的类型分配类型时出错类型不兼容 - error incompatible types when assigning to type from type in C 错误:从类型&#39;digestInfo&#39;分配给类型&#39;char *&#39;时类型不兼容 - error: incompatible types when assigning to type ‘char *’ from type ‘digestInfo’ 错误:分配和返回类型时类型不兼容 - Error: incompatible types when assigning to and returning to type 从可可项目中的不兼容类型“ void *”错误分配给“ MyPrivateData *” - Assigning to “MyPrivateData *” from incompatible type 'void *' error in cocoa project 错误:语义问题从不兼容类型“ void”分配给“ int” - Error: Semantic issue Assigning to 'int' from incompatible type 'void' 从int类型分配给char []类型时,类型不兼容 - Incompatible types when assigning to type char[] from type int 从Int类型分配给&#39;struct PartyInfo&#39;类型时不兼容的类型 - Incompatible types when assigning to type 'struct PartyInfo' from type Int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM