0

I have the following code:

#include <iostream>

using std::cout;
using std::endl;

void display(const int* start, const int* end);

int main()
{
    int numbers[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    display(numbers, numbers + 9);

    system("pause");
    return 0;
}

void display(const int* start, const int* end)
{
    const int* ptr;
    for (ptr = start; ptr != end; ptr++)
    {
        cout << *ptr << endl;
    }
}

The output is correct and works as I expected to work.

But in the display function, I have specifically mentioned that my "ptr" variable is a constant which means that the value of the "ptr" variable cannot change and hence, it will only refer to one address at all times.

Looking at the for loop, I am able to increment the ptr variable (Why?).

So, out of curiosity I removed the const keyword from const int* ptr line to see what happens and it gives me the following error:

for (ptr = start; ptr != end; ptr++) // a value of type "const int*" cannot be assigned to an entity of type "int *"
{
    cout << *ptr << endl;
}

The error is for ptr = start;
That gave me new questions:

  1. My ptr variable after modifications is NOT a const int* so why is it saying that it is a const int *?
  2. I am assigning my start variable (const int ) to a ptr variable (const int ), but it thinks that I am trying to assign my start variable of type int to a const int which is the ptr variable?
  3. Even if this does not work, then why does incrementing work when my ptr was constant before?

I looked at the following two links to understand it further but it does not answer my question:
1. Why this Error: a value of type "const int*" cannot be assigned to an entity of type "int*"?
2. How pointer increment works



EDIT The reason why my question is not the duplicate of this link (1. Why this Error: a value of type "const int*" cannot be assigned to an entity of type "int*"?) is because this is what he is assigning a constant pointer's value to another pointer which is also pointing to another non constant value and hence why. In my case I initialized a constant integer pointer and I am still able to increment it. I do not think that we can increment constant variables?

13
  • 2
    const int * p is a non constant pointer to a constant int. Does this not answer your question? stackoverflow.com/questions/34542470/… Commented Dec 15, 2020 at 10:52
  • 1
    Does this answer your question? Why this Error: a value of type "const int*" cannot be assigned to an entity of type "int*"? Commented Dec 15, 2020 at 10:53
  • 1
    The type you seem to think you have - const pointer to int - is spelled int * const. Commented Dec 15, 2020 at 10:55
  • 2
    the pointer is not constant. const int * p points to a const int, but p is not constant Commented Dec 15, 2020 at 10:58
  • 3
    Your question is based on a false premise. You're assuming that const int *p is a const pointer to an int (i.e. the value of the pointer can't change). Whereas it is a pointer to a const int (i.e. what it points to cannot change). So p can be changed (so it points at another int) but it can't be used to change its pointee. This means p++ is valid, but *p = 42 is not. Commented Dec 15, 2020 at 11:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.