I just wonder why ptr and p1 have the same type uint8_t but this code below warning that "assignment from incompatible type"
uint8_t *ptr=NULL;
uint8_t *p1=NULL;
ptr=&p1;
but when I type cast p1 from uint8_t to uint8_t that warning disappears
uint8_t *ptr =NULL;
uint8_t *p1=NULL;
ptr=(uint8_t*)&p1;
please help me I'm stuck on that issue for so long
CodePudding user response:
You have declared two variables, both of the same type: uint8_t *. This means they are both supposed to hold addresses of uint8_t objects. Since both variables are of the same type you can assign one to the other:
ptr = p1; // Valid
However, the & operator messes things up. p1 is already a pointer, so it is supposed to hold the address of a uint8_t. But &p1 creates a pointer to p1, i.e. a pointer-to-a-pointer to a uint8_t. &p1 has type uint8_t ** (notice the second *). It is supposed to hold the address of the address of a uint8_t.
therefore, there is a type mismatch. ptr holds a uint8_t *; &p1 holds a uint8_t **.
Removing the ampersand will fix this.
CodePudding user response:
You're assigning a uint8_t ** to a uint8_t *. Fix the issue by removing the ampersand:
uint8_t *ptr=NULL;
uint8_t *p1=NULL;
ptr=p1;
