1

Why there is a need to typecast an address when a pointer is pointing to an address?

Example

uint32_t *ptr = (uint32_t *) 0x40008000;

What will be the result if we don't typecast the address?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
anigam
  • 11
  • 1
  • 3
    `0x40008000` is an *integer* (literal); the cast is to inform the compiler to use the integer *as an address*. – user2864740 Oct 10 '20 at 05:14
  • 3
    With gcc or clang, you should be compiling with the `-Wall -Wextra -Werror` options. Then the answer to this question becomes self evident. – user3386109 Oct 10 '20 at 05:31
  • If you don't cast it is a constraint violation, because the program is invalid. Unfortunately C compilers are allowed to compile invalid programs too, provided that they issue a doagnostic message (i.e. "a warning") – Antti Haapala -- Слава Україні Oct 10 '20 at 06:42

0 Answers0