简体   繁体   中英

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.

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. So to me it would seem that the while loop is saying to cout aryC so long as the two pointers are equal. But, both pointers are being incremented by one, which I take to mean which char value in the array is being looked at. 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; we only know there are 20 values in the array. So how can you compare aryA and aryC in the first place?

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.

It's quite easy, *aryB++ = *aryA++ can be seen as

*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.

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 .

aryB = new char[20]; sets aryB to a new character array.

aryC = arB; sets aryC as a reference to 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). This also changes the value of aryC, but not what it points to. In the end, aryA is copied into aryC.

aryB is a pointer that points to an address in memory. By placing * in front of pointer (*aryB), you access the actual data at that memory address. ++ increments the pointer by 1, which makes it to point to the next memory address. Inside while() you do not compare (the operator is not ==), the assignment operator is used (=). This means that you will be copying data from memory aryA to aryB. Also aryC = aryB means that aryC points to the same memory address as aryB (points to the first element of the array). In other words by modifying data at aryB, you also modify it for 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.

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:

cout << aryC << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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