Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 250 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the difference between const int*, const int * const, and int const *?

#1
I always mess up how to use `const int*`, `const int * const`, and `int const *` correctly. Is there a set of rules defining what you can and cannot do?

I want to know all the do's and all don'ts in terms of assignments, passing to the functions, etc.
Reply

#2
This question shows **precisely** why I like to do things the way I mentioned in my question [is const after type id acceptable?][1]


In short, I find the easiest way to remember the rule is that the "const" goes *after* the thing it applies to. So in your question, "int const *" means that the int is constant, while "int * const" would mean that the pointer is constant.

If someone decides to put it at the very front (eg: "const int *"), as a special exception in that case it applies to the thing after it.

Many people like to use that special exception because they think it looks nicer. I dislike it, because it is an exception, and thus confuses things.

[1]:

[To see links please register here]

Reply

#3
There are many other subtle points surrounding const correctness in C++. I suppose the question here has simply been about C, but I'll give some related examples since the tag is C++ :

- You often pass large arguments like strings as `TYPE const &` which prevents the object from being either modified or copied. Example :

`TYPE& TYPE::operator=(const TYPE &rhs) { ... return *this; }`

But `TYPE & const` is meaningless because references are always const.

- You should always label class methods that do not modify the class as `const`, otherwise you cannot call the method from a `TYPE const &` reference. Example :

`bool TYPE::operator==(const TYPE &rhs) const { ... }`

- There are common situations where both the return value and the method should be const. Example :

`const TYPE TYPE::operator+(const TYPE &rhs) const { ... }`

In fact, const methods must not return internal class data as a reference-to-non-const.

- As a result, one must often create both a const and a non-const method using const overloading. For example, if you define `T const& operator[] (unsigned i) const;`, then you'll probably also want the non-const version given by :

`inline T& operator[] (unsigned i) {
return const_cast<char&>(
static_cast<const TYPE&>(*this)[](i)
);
}`

Afaik, there are no const functions in C, non-member functions cannot themselves be const in C++, const methods might have side effects, and the compiler cannot use const functions to avoid duplicate function calls. In fact, even a simple `int const &` reference might witness the value to which it refers be changed elsewhere.
Reply

#4
1. **Constant reference:**

A reference to a variable (here int), which is constant. We pass the variable as a reference mainly, because references are smaller in size than the actual value, but there is a side effect and that is because it is like an alias to the actual variable. We may accidentally change the main variable through our full access to the alias, so we make it constant to prevent this side effect.

int var0 = 0;
const int &ptr1 = var0;
ptr1 = 8; // Error
var0 = 6; // OK

2. **Constant pointers**

Once a constant pointer points to a variable then it cannot point to any other variable.

int var1 = 1;
int var2 = 0;

int *const ptr2 = &var1;
ptr2 = &var2; // Error

3. **Pointer to constant**

A pointer through which one cannot change the value of a variable it points is known as a pointer to constant.

int const * ptr3 = &var2;
*ptr3 = 4; // Error

4. **Constant pointer to a constant**

A constant pointer to a constant is a pointer that can neither change the address it's pointing to and nor can it change the value kept at that address.

int var3 = 0;
int var4 = 0;
const int * const ptr4 = &var3;
*ptr4 = 1; // Error
ptr4 = &var4; // Error

Reply

#5
I had the same doubt as you until I came across this [book][1] by the C++ Guru Scott Meyers. Refer the third Item in this book where he talks in details about using `const`.

Just follow this advice

1. If the word `const` appears to the left of the asterisk, what's pointed to is constant
2. If the word `const` appears to the right of the asterisk, the pointer itself is constant
3. If `const` appears on both sides, both are constant

[1]:

[To see links please register here]

Reply

#6
Like pretty much everyone pointed out:

[What’s the difference between `const X* p`, `X* const p` and `const X* const p`?][1]

> You have to read pointer declarations
> right-to-left.
>

> * `const X* p` means "p points to an X that is const": the X object can't be changed via p.

> * `X* const p` means "p is a const pointer to an X that is non-const": you can't change the pointer p itself, but you can change the X object via p.

> * `const X* const p` means "p is a const pointer to an X that is const": you can't change the pointer p itself, nor can you change the X object via p.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through