Kay
Kay

Reputation: 131

Pointer comparison warning

I'm making a 16bit checksum of all words in a memory space from 0 to 0x0020000 with:

uint16_t checksum = 0;
    for (uint16_t * word_val = 0; word_val < 0x0020000ul; word_val++)
    {
        checksum += *word_val;
    }

I receive the warning "comparison between pointer and integer". How can I make this warning go away?

Upvotes: 0

Views: 64

Answers (1)

Jo&#235;l Hecht
Jo&#235;l Hecht

Reputation: 1842

word_val is a pointer, 0x0020000ul is an integer, and comparing them leads to a warning.

To prevent this warning, just cast the integer constant to a uint16_t pointer, in order to compare datas of the same type :

uint16_t checksum = 0;
for (uint16_t * word_val = 0; word_val < (uint16_t *)0x0020000ul; word_val++)
{
    checksum += *word_val;
}

Upvotes: 3

Related Questions