3

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!
user548800
  • 71
  • 2
  • 2
  • 4

7 Answers7

3

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.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • This is true, but what he wants to do seems, and I can't believe I'm finally seeing this as the best phrase to describe it, awfully smelly. – San Jacinto Dec 20 '10 at 19:45
  • codeySmurt: Yes, you are correct. &the_go_status is a pointer to an int. – Jonathan Wood Dec 20 '10 at 19:54
  • to be more specific &the_go_status is the address to what the pointer, if it had been declared, points. – st-h Dec 21 '10 at 02:15
  • So, for int * pointer; pointer = &anotherPointer you may set a pointer to the address of the other pointer and therefore to that value. – st-h Dec 21 '10 at 02:20
3

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?

Community
  • 1
  • 1
Joey Adams
  • 41,996
  • 18
  • 86
  • 115
2

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.

Adam Maras
  • 26,269
  • 6
  • 65
  • 91
1

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; 
st-h
  • 2,444
  • 6
  • 37
  • 60
  • Just declaring a pointer doesn't do any good, you have to point it at something. – Chris Stratton Dec 20 '10 at 21:13
  • 1
    true. As for any other kind of "variable", you have to set it once, so it has a value. However, as this is one of the most basic things of programming, I assumed this being obvious – st-h Dec 21 '10 at 02:13
1
//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);
}
0

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

0
#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;
  

}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 01 '22 at 20:17