在 C 中将指针分配给枚举变量时出现问题

发布于 2024-10-08 16:37:43 字数 586 浏览 3 评论 0原文

我收到“来自不兼容指针类型的分配”的警告。我不明白为什么会出现这个警告。我不知道除了整数之外还可以将“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

回眸一笑 2024-10-15 16:37:43

因为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.

家住魔仙堡 2024-10-15 16:37:43

我不确定这是否与您的警告完全相关,但是将非常小心将对局部变量的引用分配给结构内的指针。如果 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 of error_struct outside the function declaring the_go_status, things will quickly break.

请帮我爱他 2024-10-15 16:37:43

这是因为 enum error_type *int * 不兼容,因为它们指向不同类型(甚至可能不同大小)的值。您应该将 the_go_status 声明为:

enum error_type the_go_status;

虽然简单地转换指针(即 (enum error_type *)&the_go_status)将使警告消失,但它可能会导致某些错误平台。请参阅 sizeof(enum) == sizeof(int) 总是吗?

This is because enum error_type * isn't compatible with int *, because they point to values of different types (and possibly even different sizes). You should declare the_go_status as:

enum error_type the_go_status;

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?

默嘫て 2024-10-15 16:37:43

如果你想使用指针,你应该声明一个指针:

int * the_go_status

否则你声明一个原语,它不是放在堆上,而是放在堆栈上。 (如果错误,请纠正我)

但是,我根本不明白你为什么要使用指针。只需在结构定义中执行类似的操作:

enum error_type status;

并将最后一行更改为:

error_struct.status = the_go_status; 

you should declare a pointer if you want to use a pointer:

int * the_go_status

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:

enum error_type status;

and change your last line to:

error_struct.status = the_go_status; 
锦欢 2024-10-15 16:37:43
//This might be the simplest 

#include<stdio.h>
typedef enum {err_1=0,err_2=1,err_3=2,err_4=4}error; 
typedef struct
{
    int val;
    error* status;

}errval;

int main() {

    error the_go_status=err_1;  
    errval val1;//just a variable name for the struct
    val1.status=&the_go_status;
    printf("%d",val1.status);
}
//This might be the simplest 

#include<stdio.h>
typedef enum {err_1=0,err_2=1,err_3=2,err_4=4}error; 
typedef struct
{
    int val;
    error* status;

}errval;

int main() {

    error the_go_status=err_1;  
    errval val1;//just a variable name for the struct
    val1.status=&the_go_status;
    printf("%d",val1.status);
}
め七分饶幸 2024-10-15 16:37:43

“the_go_status”应输入“enum error_type”。
你可以 typedef 枚举

The "the_go_status" should be typed "enum error_type".
You could typedef the enum

静赏你的温柔 2024-10-15 16:37:43
#include <iostream>
   
using namespace std;
enum error_type  
{  
    ERR_1 = 0,  
    ERR_2 = 4,  
    ERR_3 = 78,  
    ERR_4 = 9
};  


//In a header file  
struct error_struct  
{  
   int value;  
   enum error_type *status;  
};  


    int main() {
        enum error_type the_go_status;
        the_go_status = ERR_2;
        
      
        
        cout<<the_go_status<<endl;
         
        error_struct p1;
       
        
        p1.value = 6;
        p1.status = (enum error_type *)&the_go_status;

        
        cout<<(*p1.status)<<endl;
        p1.status++;
      
        cout<<(*p1.status)<<endl;
  

}
#include <iostream>
   
using namespace std;
enum error_type  
{  
    ERR_1 = 0,  
    ERR_2 = 4,  
    ERR_3 = 78,  
    ERR_4 = 9
};  


//In a header file  
struct error_struct  
{  
   int value;  
   enum error_type *status;  
};  


    int main() {
        enum error_type the_go_status;
        the_go_status = ERR_2;
        
      
        
        cout<<the_go_status<<endl;
         
        error_struct p1;
       
        
        p1.value = 6;
        p1.status = (enum error_type *)&the_go_status;

        
        cout<<(*p1.status)<<endl;
        p1.status++;
      
        cout<<(*p1.status)<<endl;
  

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文