Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

613
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda