4

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?
Francesco Bonizzi
  • 5,142
  • 6
  • 49
  • 88
  • 3
    This does not seem to iterate over any *array*, but more specifically over a C string, which is zero-terminated. (At the end of the function it points to the character after the last one.) – Jongware Jul 19 '15 at 09:51
  • 2
    The terminology in C is a little mixed up for some terms. For example the term "null" can have two meanings, either as a null *pointer* or the null *character terminating a string. For your loop condition what is tested is not the null pointer but the null character. – Some programmer dude Jul 19 '15 at 09:53
  • 3
    Google "pointer arithmetic". – n. m. could be an AI Jul 19 '15 at 09:55
  • 3
    And regarding the `p++` expression, search for and read about *pointer arithmetic*. – Some programmer dude Jul 19 '15 at 09:55

7 Answers7

4

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

Community
  • 1
  • 1
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
2

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.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
2

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)

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
2

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

If you meant 0 or '\0', yes.

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?

It advances the pointer to the next location that makes sense, thus according to pointer arithmetic. Yes, it contains a number which represents the address of the pointed object. Exactly; further, you cannot change the pointed value (*p) because it's const.

edmz
  • 8,220
  • 2
  • 26
  • 45
2

This is really basic C pointer usage.

const char *p means p in a pointer to const char => p can be modified but not *p

while (*p) is *by definition the same as while (*p != 0) so yes the loop does the test *p == 0 on each iteration

++p increases pointer and as such is legal per my first remark

Conclusion : this code loops through a char array until pointed value is null - the definition of the end of a C string (null terminated char array)

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
2

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

Correct

p is a pointer containing "addresses of type char". An address is a byte offset (offsets from 0).

p + 1 will increment to point to the next char address in memory. Since char is one byte long p will increment its byte offset by one. (In case of an int32 it will increment by 4, for example). If p was initially 30, p+1 will be 31 for char, and 34 for int32 pointers.

In this loop p changes. Since *p is the value contained at the memory address pointed by p, it will also change (in general, it could be the case that it randomly keeps constant).

earizon
  • 2,099
  • 19
  • 29
2

while(*p) This reads byte/char pointed to by p and compares the read value against 0. That is it checks whether *p is equal to zero or not.

++p; This is same as p = p + 1; This increments p to point to next char.

In effect, since p points to an array of chars, the loop helps you iterate through all chars till it finds a null char. If p is a pointer to a string, this iterates through all chars in the string. null char (\0) is where a string ends.

| h | e | l | l | o | 0 |

Assume the string "hello", and p points to h, the loop iterates 5 times (for h, e, l, l, o), and comes out when it sees 0.

Seema Kadavan
  • 2,538
  • 1
  • 16
  • 31