C - 将结构设置为 null(赋值中的类型不兼容) [英] C - Setting a struct to null (incompatible types in assignment)

查看:65
本文介绍了C - 将结构设置为 null(赋值中的类型不兼容)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构:

struct elem {
  int number;
  char character;
};

struct item {
  struct elem element;
};

以及以下函数:

void init(struct item *wrapper) {
  assert(wrapper != NULL);
  wrapper->element = NULL;
}

item->element = NULL 产生 赋值中的不兼容类型.这是为什么?将结构体设置为 NULL 不应该吗?

item->element = NULL yields a incompatible types in assignment. Why is that? Shouldn't setting a struct to NULL be okay?

推荐答案

在C中NULL一般定义如下

#define NULL ((void*)0)

这意味着它是一个指针值.在这种情况下,您尝试将指针 (NULL) 分配给非指针值 item::element 并获取适当的消息.似乎您的意图是让 element 成为此处的指针,因此请尝试以下操作

This means that it's a pointer value. In this case your attempting to assign a pointer (NULL) to a non-pointer value item::element and getting the appropriate message. It seems like your intent is to have element be a pointer here so try the following

struct item {
  struct elem* element;
};

这篇关于C - 将结构设置为 null(赋值中的类型不兼容)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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