Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code works, but throws Incompatible Pointer Type warning

Trying things out as I learn C code, I wanted to test something. It worked as expected, but throws the warning

Warning 1 assignment from incompatible pointer type [enabled by default]

The code is simple. All I am doing here is toggling PIN B7 on an atmega2560. I have a LED hooked to it and I can see it blinking, so I know it works as expected.

Can anyone explain why I am seeing this error, even though it is executed as expected? The code is as follows:

#include <avr/io.h>
#include <util/delay.h>

void main(void) {
    int *ptr;
    ptr = &PORTB; // This line throws the warning

    DDRB = (1 << 7);

    while(1) {
        *ptr = (1 << 7);
        _delay_ms(1000);

        *ptr = (0 << 7);
        _delay_ms(1000);
    }
}

PORTB is an 8bit register that has a bit per pin to control whether that pin is HIGH or LOW.

For now, I am glad it works. But these warnings annoy me.

like image 897
Joey van Hummel Avatar asked Dec 07 '22 06:12

Joey van Hummel


1 Answers

int *ptr;
ptr = &PORTB; // This line throws the warning

PORTB is a volatile unsigned char defined with something like this:

*(volatile unsigned char *) 0xBEEF

Change your ptr declaration to a volatile unsigned char *:

volatile unsigned char *ptr;
ptr = &PORTB;
like image 134
ouah Avatar answered Dec 28 '22 10:12

ouah