Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

600
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!