简体   繁体   English

如何用指针理解字符串

[英]How to understand strings with pointers

I have been studying the C language for the last few months.最近几个月我一直在学习 C 语言。 I'm using a book and I have this exercise:我正在使用一本书,我有这个练习:

char vector[N_STRINGS][20] = {"ola", "antonio", "susana"};
char (*ptr)[20] = vector;
char *p;

while(ptr-vector<N_STRINGS)
{
    p = *ptr;
    while(*p)
        putchar(*p++);
    putchar('\n');
    ptr++;
}

I understand everything expect the while(*p) !我明白一切都期待while(*p) I can't figure out what the while(*p) is doing.我无法弄清楚while(*p)在做什么。

The variable p in your code is defined as a pointer to a char .代码中的变量p被定义为指向char指针 The get the actual char value that p points to, you need to dereference the pointer, using the * operator.获取p指向的实际char值,您需要使用*运算符取消引用指针。

So, the expression in your while loop, *p evaluates - at the beginning of each loop - to the char variable that p is currently pointing to.因此, while循环中的表达式*p在每个循环开始时计算为p当前指向的char变量。 Inside the loop, the putchar call also uses this dereference operator but then increments the pointer's value so, after sending that character to the output, the pointer is incremented (the ++ operator) and it then points to the next character in the string.在循环内部, putchar调用也使用了这个解引用运算符,但随后会增加指针的值,因此,在将该字符发送到输出后,指针会增加( ++运算符),然后它指向字符串中的下一个字符。

Conventionally (in fact, virtually always), character strings in C are NUL -terminated, meaning that the end of the string is signalled by having a character with the value of zero at the end of the string.传统上(实际上,几乎总是), C中的字符串是以NUL结尾的,这意味着字符串的结尾是通过在字符串末尾具有一个值为零的字符来表示的。

When the while loop in your code reaches this NUL terminator, the value of the expression *p will thus be ZERO.当代码中的while循环到达此NUL终止符时,表达式*p的值将因此为零。 And, as ZERO is equivalent to a logical "false" in C (any non-zero value is considered "true"), the while loop will end.而且,由于零相当于C的逻辑“假”(任何非零值都被视为“真”), while循环将结束。

Feel free to ask for further clarification and/or explanation.随时要求进一步澄清和/或解释。

From the C Standard (6.8.5 Iteration statements)来自 C 标准(6.8.5 迭代语句)

4 An iteration statement causes a statement called the loop body to be executed repeatedly until the controlling expression compares equal to 0. 4 迭代语句会导致重复执行称为循环体的语句,直到控制表达式比较等于 0。

In this part of the program在这部分程序中

p = *ptr;
while(*p)
//…

the pointer p points to the first character of a current string.指针p指向当前字符串的第一个字符。 String in C is a sequence of characters terminated by the zero character '\\0' . C 中的字符串是由零字符'\\0'终止的字符序列。

So let's for example the pointer initially points to the first character of the string "ola" .因此,让我们举个例子,指针最初指向字符串"ola"的第一个字符。 The string is represented in the corresponding character array like该字符串在相应的字符数组中表示,如

{ 'o', 'l', 'a', '\0' }

The condition in the loop循环中的条件

while(*p)

may be rewritten like可以重写为

while(*p != 0 )

So the loop will be executed for all characters of the string except the last zero-terminated character and there will be outputted the first three characters of the string.因此,将对字符串中除最后一个以零结尾的字符之外的所有字符执行循环,并输出字符串的前三个字符。

Pay attention to that (6.5.9 Equality operators)注意(6.5.9 相等运算符)

3 The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence.108) Each of the operators yields 1 if the specified relation is true and 0 if it is false. 3 ==(等于)和!=(不等于)运算符与关系运算符类似,只是它们的优先级较低。108)如果指定的关系为真,则每个运算符都会产生 1,如果指定的关系为假,则产生 0。 The result has type int.结果的类型为 int。 For any pair of operands, exactly one of the relations is true.对于任何一对操作数,只有一个关系为真。

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

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