1

I would like to know if there is any difference in the usage of const between variable and pointer, like in the following:

const int var;
const int *ptr;
Gerhard
  • 6,850
  • 8
  • 51
  • 81
Thilak
  • 70
  • 10
  • 3
    `var = 42;` is illegal ... `ptr = &var;` is legal... `*ptr = 42;` is illegal – pmg Oct 12 '21 at 06:58
  • 1
    The first one says that you can't change `var`, the second one says that you can't change the data that `ptr` points to. – Barmar Oct 12 '21 at 06:59
  • 1
    For more fun: [What is the difference between const int*, const int * const, and int const *?](https://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const) – Retired Ninja Oct 12 '21 at 07:00
  • @RetiredNinja Like in pointers is there any difference between const int var, and int const var ? – Thilak Oct 12 '21 at 07:03
  • @poorna No, there is not. – Retired Ninja Oct 12 '21 at 07:27

0 Answers0