In C , there are two different ways to declare a constant integer variable:
<code class="cpp">int const x = 3; const int x = 3;</code>
Are these two declarations equivalent? Yes, they both declare a constant integer variable named x with the value 3. So, you can use either one of them.
However, when dealing with pointers, the placement of the const keyword makes a difference. For example:
<code class="cpp">int const *p = &someInt; // p points to an immutable integer const int *p = &someInt; // p is an immutable pointer to an integer</code>
In the first declaration, the const keyword is applied to the pointer type, indicating that the pointer itself cannot be modified. In other words, p cannot be assigned a different address.
In the second declaration, the const keyword is applied to the integer type, indicating that the value pointed to by p cannot be modified. In other words, the integer that p points to cannot be changed.
The above is the detailed content of int const vs. const int: What\'s the Difference with Pointers?. For more information, please follow other related articles on the PHP Chinese website!