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!

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
codeySmurt: Yes, you are correct. &the_go_status is a pointer to an int.
to be more specific &the_go_status is the address to what the pointer, if it had been declared, points.
So, for int * pointer; pointer = &anotherPointer you may set a pointer to the address of the other pointer and therefore to that value.
3

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.

Comments

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?

Comments

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; 

2 Comments

Just declaring a pointer doesn't do any good, you have to point it at something.
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
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);
}

1 Comment

this will give compilation error while printf(),
0

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

Comments

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;
  

}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.