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

603
Visualizações
What's the definition of ++p where p is const char *p in C?

If I need to iterate over an array in C, I can do something like:

void uselessTest(const char *p)
{
    while(*p)
    {
        ++p;
    }
}

I'm wondering about what that means in detail:

  • Does it tests, at every loop, if (*p != null) ?
  • If it does p = p + 1, what does that mean? Is p a numeric value? *p should be the value pointed and p the address of the pointer? So in this loop p changes while *p remains the same?
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Does it tests, at every loop, if (*p != null) ?

I would say more correct is to say it checks if *p!=0.

If it does p = p + 1, what does that mean? Is p a numeric value?

It appears C standard does not define what pointer is internally. But in below example, I will assume p holds an address represented as integer (which usually is the case). In that case, in your above example, when you add one to it, p moves to the next address, e.g., if p was 0x1000, after increment it would become 0x1001 (This is because size of char is 1, in case p was pointer to int it would move four bytes forward* -given size of integer is four).

So in this loop p changes while *p remains the same?

No, indeed *p tries to dereference p, in other words, obtain contents of the object stored at address p. So of course if you change value of p, you may also get different content at new address. e.g., if initial value of p was 0x1000, after increment, it will be 0x1001, and *p will try to read value at 0x1001 which might be different from value stored at 0x1000.

* Look up pointer arithmetic for more details about that

over 4 years ago · Santiago Trujillo Relatório

0

It tests at each iteration if *p != null) and terminates the loop if it is null.

It increases the pointer, ergo, the pointer points to the next address. So using a loop like this, you can iterate over items in an array.

So basically what this loop does: it iterates over the characters in the array, until it reaches a terminating \0 character. On each iteration, the pointer p is increased to point to the next character.

over 4 years ago · Santiago Trujillo Relatório

0

Does it tests, at every loop, if (*p != null) ?

There is no null in C. There is a NUL-terminator('\0') and NULL. The latter is associated with pointers.

The check is

if (*p != 0)

which is the same as

if (*p != '\0')

If it does p = p + 1, what does that mean?

It moves the pointer sizeof(*p) bytes forward.

Is p a numeric value?

p is a const char*, or in other words, a pointer to char.

*p should be the value pointed and p the address of the pointer?

*p is the data which the pointer p points to and p is the address which the pointer points to (which is different from p's address)

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