繁体   English   中英

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

[英]Problem when assigning a pointer to an enum variable in C

我收到“来自不兼容指针类型的分配”的警告。 我不明白为什么会发生此警告。 除了 integer 之外,我不知道还有什么可以声明“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!

我收到“从不兼容的指针类型赋值”的警告。 我不明白为什么会发生这个警告。 我不知道除了整数之外还有什么可以声明“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!

我收到“从不兼容的指针类型赋值”的警告。 我不明白为什么会发生这个警告。 我不知道除了整数之外还有什么可以声明“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!

我收到“从不兼容的指针类型赋值”的警告。 我不明白为什么会发生这个警告。 我不知道除了整数之外还有什么可以声明“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!

尝试这个:

#include <stdio.h>


enum error_type
{
    ERR_1=0
    ,ERR_2=1
    ,ERR_3=2
    ,ERR_4=4
};

struct error_struct
{
    int value;
    error_type status;

};

int main(int argc, char* argv[])
{
    printf("Start\n");

    error_type the_go_status=ERR_1;
    error_struct err;

    err.value=5;
    err.status=the_go_status;


    printf("Done\n");

    getchar();


    return 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!

我收到“从不兼容的指针类型赋值”的警告。 我不明白为什么会发生这个警告。 我不知道除了整数之外还有什么可以声明“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!

我收到“从不兼容的指针类型赋值”的警告。 我不明白为什么会发生这个警告。 我不知道除了整数之外还有什么可以声明“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!
#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;
  

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM