简体   繁体   English

C ++:C字符串,指针和一个非常有趣的while循环

[英]C++: C-strings, pointers, and a very interesting while loop

I saw that a potential job interview for a C++ programmer position could ask you this question: Explain what the following C++ code segment does. 我看到一个C ++程序员职位的潜在求职面试可能会问你这个问题:解释下面的C ++代码段做了什么。

char *aryA = "Data Structures";
char *aryB, *aryC;
aryB = new char[20];
aryC = aryB;
while (*aryB++ = *aryA++);

cout << aryC << endl;

I've been looking at it for a while, but I don't think I am understanding the while loop. 我已经看了一段时间,但我不认为我理解了while循环。 So to me it would seem that the while loop is saying to cout aryC so long as the two pointers are equal. 所以对我来说,只要两个指针相等,while循环就会说cout aryC。 But, both pointers are being incremented by one, which I take to mean which char value in the array is being looked at. 但是,两个指针都增加了一个,我用它来表示正在查看数组中的哪个char值。 But if they are the same and both are being increased by one, wouldn't they always be equal? 但如果它们是相同的并且两者都增加了一个,它们不会总是相等吗? And there's another thing. 还有另外一件事。 The values for the array of chars aryB is not defined; 字符aryB数组的值未定义; we only know there are 20 values in the array. 我们只知道数组中有20个值。 So how can you compare aryA and aryC in the first place? 那你怎么能首先比较aryA和aryC呢?

If anyone can take the time to explain this code segment to me, I would really appreciate it. 如果有人可以花时间向我解释这段代码,我真的很感激。 I am having issues running visual studio, so I can't just run it myself, but even if I could I think I would still benefit from someone teaching me. 我在运行visual studio时遇到问题,所以我不能自己运行它,但即使我能想到我仍然可以从教我的人那里受益。

It's quite easy, *aryB++ = *aryA++ can be seen as 这很容易, *aryB++ = *aryA++可以被视为

*aryB = *aryA;
aryB++;
aryA++;

which just assign the character pointed by aryA to aryB and then increment both (to move on next character. The while is executed until the NUL terminating character is found, which is caught by the fact that the = operator (which is not == ) returns the assigned value. 只是将aryA指向的字符分配给aryB然后递增两者(移动到下一个字符。执行while,直到找到NUL终止字符,这是由=运算符(不是== )的事实捕获的返回指定的值。

Saving aryB to aryC before the while is just a way to keep the pointer to the beginning of the copied string, since you lose it by then incrementing aryB . 保存aryBaryC的,而之前只是一个指针保持到复制的字符串的开头,因为你那时递增失去它的方式aryB

aryB = new char[20]; sets aryB to a new character array. 将aryB设置为新的字符数组。

aryC = arB; sets aryC as a reference to aryB. 将aryC设置为aryB的引用。

while (*aryB++ = *aryA++); This one is more complicated. 这个更复杂。 It will set the current value at aryB to the current value at aryA while the current value at aryA is not false (0), then move where both pointers forward one (remember that all c strings end with \\0, which evaluates to 0). 它将aryB的当前值设置为aryA的当前值,而aryA的当前值不为false(0),然后移动两个指针前进一个(记住所有c个字符串以\\ 0结束,其值为0) 。 This also changes the value of aryC, but not what it points to. 这也改变了aryC的值,但不是它指向的值。 In the end, aryA is copied into aryC. 最后,aryA被复制到aryC中。

aryB is a pointer that points to an address in memory. aryB是一个指向内存中地址的指针。 By placing * in front of pointer (*aryB), you access the actual data at that memory address. 通过将*放在指针(* aryB)前面,可以访问该内存地址的实际数据。 ++ increments the pointer by 1, which makes it to point to the next memory address. ++将指针递增1,这使它指向下一个内存地址。 Inside while() you do not compare (the operator is not ==), the assignment operator is used (=). 在while()中你不进行比较(运算符不是==),使用赋值运算符(=)。 This means that you will be copying data from memory aryA to aryB. 这意味着您将数据从内存aryA复制到aryB。 Also aryC = aryB means that aryC points to the same memory address as aryB (points to the first element of the array). 另外aryC = aryB意味着aryC指向与aryB相同的内存地址(指向数组的第一个元素)。 In other words by modifying data at aryB, you also modify it for aryC. 换句话说,通过修改aryB上的数据,您还可以为aryC修改它。

char *aryA = "Data Structures";
char *aryB, *aryC;
aryB = new char[20];
aryC = aryB;

We can visualise the memory use and pointer contents like this: 我们可以像这样可视化内存使用和指针内容:

[(char*)aryA]--------------------------v
                                     ["Data Structures\0"]

[(char*)aryB]-------------v
                         [ 20 uninitialised chars ]
                          ^
                          |
[(char*)argC]-------------/

Then: 然后:

while (*aryB++ = *aryA++);

Is processed like this: 处理方式如下:

*aryB = *aryA    - assigns *aryB (the first uninitialised char),
                   with *aryA (the 'D' in Data) 
aryB++, aryA++   - post-increments add one to each pointer
                   (i.e. move to the 'a' in Data, the 2nd uninitialised value
while (...)      - evaluates the assignment, exiting if false
                   the assignment evaluates to the copied character
                   only character code 0 / '\0' / NUL converts to false; others to true

We now have: 我们现在有:

[(char*)aryA]---------------------------v
                                     ["Data Structures\0"]

[(char*)aryB]--------------v
                         [D                ]
                          ^
                          |
[(char*)argC]-------------/

Repeat. 重复。 This keeps copying character by character until the data from *aryA is the NUL character, which gets copied but then the assignment evaluates to false and the loop terminates. 这样就可以逐个字符地复制,直到来自* aryA的数据是NUL字符,然后将其复制,然后赋值计算为false并且循环终止。

While that was all happening, aryC stayed pointing at the start of the new -ed buffer, so it can now be used to stream out the content: 虽然这一切都在发生, aryC仍然指着new缓冲区的开头,所以它现在可以用来流出内容:

cout << aryC << endl;

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

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