Home > Backend Development > C++ > int const vs. const int: What\'s the Difference with Pointers?

int const vs. const int: What\'s the Difference with Pointers?

Mary-Kate Olsen
Release: 2024-10-26 19:08:02
Original
655 people have browsed it

int const vs. const int: What's the Difference with Pointers?

const int vs. int const

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template