Whats the deal with 'const'?

C

Chris Torek

(I was hoping someone else would answer this... no luck :) )

Although the below code is definitely poor style, is any rule actually
violated?

void f(const int *restrict p) {
*(int *) p = 0;
}
int main(void) {
int i = 1;
f(&i);
return i;
}

I think the answer to your question is "no", but I do not really
fully understand C99's "restrict".

Note that in this case, the compiler can "see" that *p is changed,
despite the "const". In my earlier example, the compiler cannot
(at least locally) see that *p is changed, even though p is declard
using "const char *p", so the compiler has to assume that the
"const" still means "changes after every subroutine call".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top