What is 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 wonder what that means in detail:

  • Does it check in every cycle if (*p != null)?
  • If it means p = p + 1what does it mean? Is p a numeric value? *pShould the value and paddress of the pointer be indicated ? So, in this cycle pchanges, but *premains unchanged?
+4
source share
7 answers

Does it check in every loop if (* p! = Null)?

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

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

. C , . , p , ( ). , , , p , , p 0x1000, 0x1001 ( , char 1, , p int, * - .)

, p , * p ?

, , *p p, , , p. , , p, . , p 0x1000, 0x1001, *p 0x1001, , 0x1000.

*

+4

, * p!= null) , .

, ergo, . , , .

, , : , \0. p , .

+2

, (* p!= null)?

C. null C. NUL- ('\0') null. .

if (*p != 0)

if (*p != '\0')

p = p + 1, ?

sizeof(*p) bytes .

p ?

p const char*, , , char.

* p , p - ?

*p - , p, p - , ( p)

+2

if (*p != null)?

0 '\ 0', .

p = p + 1, ? p ? * p , p - ? , p , * p ?

, , , . , , . ; , (*p), const.

+2

C.

const char *p , p const char = > p , *p

while (*p) * , while (*p != 0), , *p == 0

++p , ,

: char , null - C ( char)

+2

" , (* p!= null)?"

p - , " char". - ( 0).

p + 1 , char . char - , p . ( int32 , , 4). p 30, p + 1 31 char 34 int32.

p . * p - , , p, ( , ).

+2

while(*p) / char, p, 0. , * p .

++p; , p = p + 1;. p, char.

Essentially, since it ppoints to an array of characters, the loop helps you iterate over all the characters until it finds a char zero. If it pis a pointer to a string, it repeats through all the characters in the string. null char ( \0) where the line ends.

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

Suppose the string is "hello" and ppoints to h, the loop repeats 5 times (for h, e, l, l, o) and exits when it sees 0.

+2
source

Source: https://habr.com/ru/post/1598570/


All Articles