String length using pointers

后端 未结 3 517
轮回少年
轮回少年 2021-01-29 12:54

I was looking up some code tricks. I found one that I get the basics but I don\'t understand why it exits. It has to do with a string pointer in a while loop. Usually I don\'

相关标签:
3条回答
  • 2021-01-29 13:32

    while(*(s+c)) returns false when the null character is encountered and breaks the loop

    0 讨论(0)
  • 2021-01-29 13:33
    int lungh(char *p){
        char *r = p;
        while(*p++!='\0'){}
        return p-r;
    }
    
    0 讨论(0)
  • 2021-01-29 13:57

    A string is an arrary of chars, of which the very last one is the terminating character - \0. The evaluation of while checks every char starting at position indcitated by pointer s, then shifting it to the next char (next array entry) by adding c to the start address. Once \0 - the terminating character - is reached the loop breaks.

    0 讨论(0)
提交回复
热议问题