Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
Why the value of a pointer to int can't be the value of a pointer to a const int?

Why is this incorrect? The problem is when assigning b = p I get the following warning: assignment discards 'const' qualifiers from pointer target type.

int a = 4;
int *b;
const int *p = &a;
b = p;
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You have one pointer which is defined to point to an int which you allow to be changed, b; but you do not init it at first.

Then you introduce a pointer to an int which you do not allow to be changed.
And you point it to an existing int. I.e. "Look at that int, but don't you change it."

Then you try to assign the "don't touch this" pointer value to the "hey, have an int, do with what you like" pointer.
The compiler tells you that you are inconsistent there, that you are contradicting yourself and cannot really mean what you write into your code.

That is why it is incorrect. Because you violate the rules which you yourself wrote.

over 4 years ago · Santiago Trujillo Relatório

0

p is a pointer to a constant int and you're assigning it to a pointer to a non-constant int.

over 4 years ago · Santiago Trujillo Relatório

0

p is defined as a const pointer to int, which means it cannot be used to modify the int it points to. You can initialize it from the address of an int that can be modified or not. Conversely, b is a pointer to int that can be used to modify the int it points to. It should only be initialized from a pointer to int with the same ability or the address of an int that is modifiable. b = p violates this constraint, hence the warning.

Note that b = &a is OK as a is modifiable and p = b is OK too, only restricting p from being used to modify a.

You could attempt to silence the warning by casting p as (int *)p but it is not recommended: in this particular instance, p does point to an int that can be modified, but bypassing the const validation by the compiler makes the code less readable and more error prone.

Qualifying p as const int *p is a promise telling I shall not use p to modify what it points to. The need to break this promise is an indication that something is not properly structured in the program.

There is no recommended way to silence this warning, but you can simply change the code to initialize b from &a this way:

int a = 4;
int *b = &a;
const int *p = b;

With the posted order, you could casually write b = (int *)p; to try and silence the warning, but a good compiler such as clang with warnings enabled will still rightfully warn you that this cast is fishy...

If you really need to remove the constness and want to silence the warning that still occurs with the obvious b = (int *)p;, you could write this:

b = (int *)(uintptr_t)(const void *)p;  // avoid this!
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda