Home > OS >  I'm getting warning "cast to pointer from integer of different size"
I'm getting warning "cast to pointer from integer of different size"

Time:07-12

I'm getting warning "cast to pointer from integer of different size"

Machine

x86_64 GNU/Linux

Compiler

gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

This code gives me the warning.

#include <stdint.h>
#include <inttypes.h>

#define __BASE (0xE000E100)

void test(uint8_t n)
{
    int i = n / 32;

    volatile uint64_t * __nvic = ((volatile uint64_t *)(__BASE   4 * i)); //warnning
}

Another code

void test2(uint8_t n)
{
    int i = n / 32;

    volatile uint32_t * __nvic = ((volatile uint32_t *)(__BASE   4 * i)); //warning
}

I tried it; it worked.

void test3(uint8_t n)
{
    int i = n / 32;

    volatile void * ui64 = (void *)((volatile uint64_t)(__BASE   4 * i));
    volatile uint32_t * __nvic = (volatile uint32_t *)ui64;
}

Why does this code work? I don't know why.

CodePudding user response:

A pointer on your system is apparently 64 bits wide. So when you cast an expression of type int (which is typically 32 bits) to a pointer type you'll get the warning.

In the case where you don't get a warning, the int expression is first cast to uint64_t (which is the same size as a pointer) and then cast to a pointer type.

  • Related