在 C 中将指针分配给枚举变量时出现问题
我收到“来自不兼容指针类型的分配”的警告。我不明白为什么会出现这个警告。我不知道除了整数之外还可以将“the_go_status”变量声明为什么。 (注意:这不是全部代码,而只是我发布的用于说明问题的简化版本。)
警告出现在我下面包含的示例的最后一行。
//In a header file
enum error_type
{
ERR_1 = 0,
ERR_2 = 1,
ERR_3 = 2,
ERR_4 = 4,
};
//In a header file
struct error_struct
{
int value;
enum error_type *status;
};
//In a C file
int the_go_status;
the_go_status = ERR_1;
//Have the error_struct "status" point to the address of "the_go_status"
error_struct.status = &the_go_status; //WARNING HERE!
I am getting a warning of "assigment from incompatible pointer type". I don't understand why this warning is happening. I don't know what else to declare "the_go_status" variable to other than an integer. (Note: this is not all the code, but just a simplified version I posted to illustrate the problem.)
The warning occurs on the last line of the example I included below.
//In a header file
enum error_type
{
ERR_1 = 0,
ERR_2 = 1,
ERR_3 = 2,
ERR_4 = 4,
};
//In a header file
struct error_struct
{
int value;
enum error_type *status;
};
//In a C file
int the_go_status;
the_go_status = ERR_1;
//Have the error_struct "status" point to the address of "the_go_status"
error_struct.status = &the_go_status; //WARNING HERE!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
因为status是一个指向enum error_type的指针,而the_go_status是一个指向int的指针。它们是指向不同类型的指针。
Because status is a pointer to enum error_type, and the_go_status is a pointer to an int. They are pointers to different types.
我不确定这是否与您的警告完全相关,但是将非常小心将对局部变量的引用分配给结构内的指针。如果
the_go_status
是本地变量,那么一旦函数返回,对该本地变量的引用将变得无效。因此,如果您的代码(或其他人的代码)在声明the_go_status
的函数之外使用您的error_struct
实例,事情很快就会崩溃。I'm not sure if this is related exactly to your warning or not, but be very careful assigning references to local variables to pointers within structs. If
the_go_status
is a local, as soon as your function returns, the reference to that local will become invalid. So, if your code (or someone else's code) uses your instance oferror_struct
outside the function declaringthe_go_status
, things will quickly break.这是因为
enum error_type *
与int *
不兼容,因为它们指向不同类型(甚至可能不同大小)的值。您应该将the_go_status
声明为:虽然简单地转换指针(即
(enum error_type *)&the_go_status
)将使警告消失,但它可能会导致某些错误平台。请参阅 sizeof(enum) == sizeof(int) 总是吗?This is because
enum error_type *
isn't compatible withint *
, because they point to values of different types (and possibly even different sizes). You should declarethe_go_status
as:Although simply casting the pointer (i.e.
(enum error_type *)&the_go_status
) will make the warning go away, it may result in bugs on some platforms. See Is the sizeof(enum) == sizeof(int), always?如果你想使用指针,你应该声明一个指针:
否则你声明一个原语,它不是放在堆上,而是放在堆栈上。 (如果错误,请纠正我)
但是,我根本不明白你为什么要使用指针。只需在结构定义中执行类似的操作:
并将最后一行更改为:
you should declare a pointer if you want to use a pointer:
otherwise you declare a primitive, which is not placed on the heap, but on the stack. (please correct my when being wrong on that)
However, I don't get why you want to use a pointer at all. Just do something like this in your struct definition:
and change your last line to:
“the_go_status”应输入“enum error_type”。
你可以 typedef 枚举
The "the_go_status" should be typed "enum error_type".
You could typedef the enum