简体   繁体   English

指针 - 字符串

[英]Pointers - Strings

I have a code chunk which is quite meaningless for me.我有一个对我来说毫无意义的代码块。 Is there someone can explain it to me?有人可以向我解释吗?

Thanks谢谢

int strLength(char *str) {
char *p = str;
// Go until the end of the string
while (*p != ‘\0’)
p++;
return (p – str);
}

Firstly I dont get the idea behind return section of the code.首先,我不明白代码的返回部分背后的想法。

Secondly第二

char *p = str;

it didn't fit me perfectly它不适合我

Thanks in Advance提前致谢

The string is stored in memory sequentially, The idea is to find the character that ends the string (in C the accepted symbol is '\0'), you increment the pointer p until you find this symbol.字符串按顺序存储在内存中,想法是找到结束字符串的字符(在 C 中接受的符号是 '\0'),增加指针 p 直到找到这个符号。

When you find '\0' , You substract between two addresses to find the length.当您找到 '\0' 时,您在两个地址之间减去以找到长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM