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?

Programming Rage
  • 403
  • 1
  • 4
  • 18
  • 2
    `const int * p` is a non constant pointer to a constant `int`. Does this not answer your question? https://stackoverflow.com/questions/34542470/why-this-error-a-value-of-type-const-int-cannot-be-assigned-to-an-entity-of – 463035818_is_not_an_ai Dec 15 '20 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\*"?](https://stackoverflow.com/questions/34542470/why-this-error-a-value-of-type-const-int-cannot-be-assigned-to-an-entity-of) – Mureinik Dec 15 '20 at 10:53
  • No, because const int *p is pointing to a pointer that is further pointing to a non-constant. In my case I am not doing that. I am using a constant integer pointer to initialize it to a constant integer pointer. Then I am incrementing it the constant integer pointer. How can you increment a constant integer pointer? – Programming Rage Dec 15 '20 at 10:55
  • 1
    The type you seem to think you have - const pointer to `int` - is spelled `int * const`. – molbdnilo Dec 15 '20 at 10:55
  • @molbdnilo I still shouldnt be able to increment the pointer because it is "constant" – Programming Rage Dec 15 '20 at 10:57
  • 1
    1) is exactly what the first Q&A you link is about 2) is a bit confusing. Please post a complete example of the code you have questions about. Please do not post code, describe what you changed and ask about that modified code – 463035818_is_not_an_ai Dec 15 '20 at 10:57
  • 2
    the pointer is not constant. `const int * p` points to a `const int`, but `p` is not constant – 463035818_is_not_an_ai Dec 15 '20 at 10:58
  • 1
    @ProgrammingRage As has been said repeatedly in these comments, the pointer is *not* constant. The `int` that it points to is. Try `*ptr += 1;` and see what happens. Then change your prorotype to `void display(int* const start, int* const end)` and test that. – molbdnilo Dec 15 '20 at 11:01
  • @largest_prime_is_463035818 so you are saying that when I say 'const int *p' it is basically expecting a constant value and it is not a constant itself? – Programming Rage Dec 15 '20 at 11:02
  • 1
    You are confusing a `pointer to const int` with a `const pointer to int`. – super Dec 15 '20 at 11:02
  • 1
    yes thats what I am saying. `const int * p` is not constant. You can modify it as much as you like. Though it does point to a constant `int`, and you cannot modify the `int` through `p` – 463035818_is_not_an_ai Dec 15 '20 at 11:03
  • Pointers are so confusing :) But I get this! Thank you so much! – Programming Rage Dec 15 '20 at 11:03
  • 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. – Peter Dec 15 '20 at 11:03

0 Answers0