-1

What is the meaning of below two ways of declaration? Which I should use to declare a read-only register?

(1) static volatile* const uint32_t gpio_register = 0x1234ABCD;
(2) static uint32_t volatile* const gpio_register = 0x1234ABCD;

[Update]

The (1) is wrong, so do not use it

In (2) volatile has another placement with the same meaning

(3) static volatile uint32_t * const gpio_register = 0x1234ABCD;
  • 2
    Your first example won't compile: https://godbolt.org/z/W9EcTEvaT – mediocrevegetable1 Oct 09 '21 at 08:52
  • 1
    Do you really want a pointer to a read-only register? A read-only pointer to a register? Or a read-only pointer to a read-only register? Either way, read [https://www.dansaks.com/articles/1998-06%20Placing%20const%20in%20Declarations.pdf](https://www.dansaks.com/articles/1998-06%20Placing%20const%20in%20Declarations.pdf) and [https://www.dansaks.com/articles/1998-08%20What%20const%20Really%20Means.pdf](https://www.dansaks.com/articles/1998-08%20What%20const%20Really%20Means.pdf) – kkrambo Oct 11 '21 at 14:30

1 Answers1

3

It should be:

volatile const uint32_t * gpio_register;

meaning that gpio_register points to a volatile constant variable. Some variations on the left of the * are probably possible, like const volatile uint32_t.

If you put const after *, the pointer itself becomes constant, which is probably not you want.

Use of static is optional and depends on the use case.

Tagli
  • 2,412
  • 2
  • 11
  • 14
  • Re “Some variations on the left of the * are probably possible”: All of them are possible. [The C standard permits the declaration specifiers to be in any order.](https://stackoverflow.com/a/60439186/298225) You can even split them and write `long const long` for a `long long` that is `const`. – Eric Postpischil Oct 09 '21 at 11:12
  • Use of `static` should not be described as optional, because that suggests it has no effect. It changes the linkage from external to internal, so it should be used if and only if one wants the identifier to have internal linkage. Also, it changes the declaration from a tentative definition to a regular definition. – Eric Postpischil Oct 09 '21 at 11:13