3 ms·
The following is a foo function that take a constant pointer: void foo(int *const x); If you point x to another adress inside foo function, it will not co
by peeyek 10y ago
The following is a foo function that take a constant pointer:
void foo(int *const x);
If you point x to another adress inside foo function, it will not compiled.
The author seems think that
void foo(cont int *x);
is function that takes a constant pointer which is wrong, it is a function that takes pointer to constant object. In this case, it is legal if you point x to another address in memory inside foo function.
- heinrich5991 10y agoWhere do you see this confusion? I don't think it's there. The author points out that it is not necessarily undefined behavior to take your second prototype, cast the const away and modify the pointed-to object if you ensure that it is only called with non-constant objects.
- peeyek 10y agoIn 2nd paragraph, the author states: void foo(const int *); ... > The function foo takes a const pointer, which is a promise from the author of foo that it won’t modify the value of x. Given this information, it would seem the compiler may assume x is always zero, and therefore y is always zero.
- Sharlin 10y agoIt is clear that the author means "pointer to const" there.
- cygx 10y agoThe author does not think that, all he does is point out correctly that const-qualifying a pointer's target type cannot be used for optimization purposes as it does not make any actual guarantees about object mutability. Note that in contrast, restrict-qualifying a ponter-to-const (ie `const int *restrict x`) does make such guarantees, but only callee-side.
- peeyek 10y agoUpdate: The author have been corrected the 2nd paragraph. My comment above is not valid anymore