Home > Net >  how does this while loop sort out pointers?
how does this while loop sort out pointers?

Time:10-04

I am a complete novice to pointers and discovered them about a week ago. This book I am reading introduced me a problem for me to solve and it took me a while without coming up with the correct solution, and so I cheated. Looked it up and came up with this:

#include <iostream>

void mystrcpy(char* str1, char* str2) {
    while (*str1   = *str2  );

}

int main() {
    char str1[15] = "C  ";
    char str2[15] = "Pointers";

    std::cout << str1 << std::endl << str2 << std::endl;

    mystrcpy(str1, str2);

    std::cout << str1 << std::endl << str2 << std::endl;
}

Question was me to fill in the blanks at the void function to solve the problem and the problem beneath at main() which printed out this:

C  
Pointers
C  
Pointers

and it wanted me to look like this:

C  
Pointers
Pointers
Pointers

So, basically I do not understand how while (*str1 = *str2 ); solves this? Been looking at it for a while still clueless how it sorst the strings out to output it like that

CodePudding user response:

Pointers are like variables that point to a specific location in memory. You can dereference them to get the value that they point to with *str2, and increment them to make them point to the next value in memory (based on their datatype): str2 .

The precedence can be confusing. *str2 works in this order:

  • get the value pointed to by str2, and then
  • increment str2 to point to the next character
    while (*str1   = *str2  );

This statement copies each char from str2 into str1 until it reaches a null pointer in str2. It works by:

  • *str2: get the character pointed to by str2
  • *str1 = *str2: write the character pointed to by str2 to the destination pointed to by str1
  • str1 , str2 : increment str1 and str2 for the next loop iteration. Now each pointer points to the next character in each string.
  • When str2 points to a null terminator, *str2 returns 0 (or '\0'), and *str1 = *str2 will write it to str1, and also return the 0 which will cause the while loop to complete.

CodePudding user response:

Each iteration, the loop assigns the value of the second element of array to the first element of the array, after which it checks whether the left set value is equal to zero, and if not, the loop continues. At the end of the iteration, the loop increments both variables.

CodePudding user response:

str1 starts as C , and str2 starts as Pointers. Then the mystrcopy function is called which copies the value of str2 into str1.

The copying is done by the line in question:

while (*str1   = *str2  );

I agree that this line is relatively opaque, but here are some basics:

  • The * on each "de-references` each pointer. That means it is looking at the value stored at the given address, not the address itself.
  • The is used to shift the pointer address from its current location to the next location in the array. It is placed after the pointer so that the pointer is incremented after we get/set it's value.
  • The while-loop here has only a condition, however the condition itself is an action. The action is executesd and the result is checked. When the end of the string is reached, the \0 char is found which terminates the loop.

So, in summary, the line copies a single char value from str2 into the value of str1, increments both pointers to point to the next char of each, then repeats until failure.

It is worth noting that, although the pointers are being changed within the function to point at new addresses, not just the start of each array, these pointer variables are not passed into the function by reference, and so the pointer variables outside the function remain unchanged, still containing the address of the first char. However, the data stored at those addresses has been modified.

  • Related