得到C错误:转换为请求的非标量类型

use*_*276 4 c struct pointers

嘿我收到这个错误:

error: conversion to non-scalar type requested
Run Code Online (Sandbox Code Playgroud)

这是我的结构:

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;
};
Run Code Online (Sandbox Code Playgroud)

这是给我问题的代码:

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))
Run Code Online (Sandbox Code Playgroud)

我不是想在结构上使用calloc吗?此外,我有一个我已经创建的结构,然后我想将其设置为相同结构类型的指针,但得到一个错误.这是我正在做的一个例子:

struct value x;
struct value* y = *x;
Run Code Online (Sandbox Code Playgroud)

这给了我这个错误

error: invalid type argument of ‘unary *’
Run Code Online (Sandbox Code Playgroud)

当我做y = x时,我收到这个警告:

warning: assignment from incompatible pointer type
Run Code Online (Sandbox Code Playgroud)

wil*_*ser 13

您正在尝试将指针表达式(malloc(的返回类型)和friends为void*)分配给结构类型(struct new_node).那是胡说八道.另外:不需要演员表(可能很危险,因为它可以隐藏错误)

struct key *new_node = calloc(1, sizeof *new_node);
Run Code Online (Sandbox Code Playgroud)

与其他malloc()行相同的问题:

struct value *head_value = calloc(1, sizeof *head_value);
Run Code Online (Sandbox Code Playgroud)

更多错误:你省略了'struct'关键字(在C++中是允许的,但在C中是无意义的):

struct key{
   int x;
   struct value *values;
   struct key *next;
   struct key *prev;
};
Run Code Online (Sandbox Code Playgroud)

更新:使用struct的结构和指针.

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);
Run Code Online (Sandbox Code Playgroud)