Back to questions
JuniorTheoryVery common
Which line of this program does not compile?
int main() {
int a = 1;
int b = 2;
const int* p = &a;
int* const q = &b;
p = &b;
*p = 4;
*q = 3;
return a + *p;
}*p = 4;: p cannot change the int it points to
- Expecting
p = &b;to fail: inconst int* ptheconstbelongs to theint, sopitself may point elsewhere. - Expecting
*q = 3;to fail: inint* const qtheconstbelongs to the pointer, so theintmay change throughq.
- How would you declare a pointer where neither the pointer itself nor the value it points to can change?
- Why does a function that only reads numbers take
const int* datarather thanint* data?