What is the difference between these declarations in C?

后端 未结 2 1411
一整个雨季
一整个雨季 2021-01-01 03:14

In C and C++ what do the following declarations do?

const int * i;
int * const i;
const volatile int ip;
const int *i;

Are any of the above

2条回答
  •  星月不相逢
    2021-01-01 03:56

    You read variables declarations in C/C++ right-to-left, so to speak.

    const int *i;  // pointer to a constant int (the integer value doesn't change)
    
    int *const i;  // constant pointer to an int (what i points to doesn't change)
    
    const volatile int ip;  // a constant integer whose value will never be cached by the system
    

    They each have their own purposes. Any C++ textbook or half decent resource will have explanations of each.

提交回复
热议问题