嘿,我收到这个错误:
error: conversion to non-scalar type requested
这是我的结构:
typedef struct value_t value;
struct value{
void* x;
int y;
value* next;
value* prev;
};
typedef struct key_t key;
struct key{
int x;
value * values;
key* next;
key* prev;
};
这是给我带来问题的代码:
struct key new_node = (struct key) calloc(1, sizeof(struct key));
struct key* curr_node = head;
new_node.key = new_key;
struct value head_value = (struct value) calloc(1, sizeof(struct value))
难道我不应该在结构上使用 calloc 吗?另外,我有一个我创建的结构,然后我想将它设置为相同结构类型的指针,但出现错误。这是我正在做的一个例子:
struct value x;
struct value* y = *x;
这给了我这个错误
error: invalid type argument of ‘unary *’
当我执行 y = x 时,我收到此警告:
warning: assignment from incompatible pointer type
最佳答案
您正在尝试将指针表达式(malloc()
和 friend 的返回类型是 void*
)分配给结构类型(struct new_node
)。那是无稽之谈。另外:不需要强制转换(并且可能很危险,因为它可以隐藏错误)
struct key *new_node = calloc(1, sizeof *new_node);
与其他 malloc() 行相同的问题:
struct value *head_value = calloc(1, sizeof *head_value);
更多错误:您省略了“struct”关键字(这在 C++ 中是允许的,但在 C 中是无意义的):
struct key{
int x;
struct value *values;
struct key *next;
struct key *prev;
};
更新:使用结构和指向结构的指针。
struct key the_struct;
struct key other_struct;
struct key *the_pointer;
the_pointer = &other_struct; // a pointer should point to something
the_struct.x = 42;
the_pointer->x = the_struct.x;
/* a->b can be seen as shorthand for (*a).b :: */
(*thepointer).x = the_struct.x;
/* and for the pointer members :: */
the_struct.next = the_pointer;
the_pointer->next = malloc (sizeof *the_pointer->next);
关于c - 得到 C 错误 : conversion to non-scalar type requested,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9012312/