1

This code is from strcpy in string.c I am trying to understand a few of the features here:

 char *(strcpy)(char *restrict s1, const char *restrict s2)
 {
     char *dst = s1;
     const char *src = s2;
     while ((*dst++ = *src++) != '\0')
         ;
     return s1;
 }

What is going on in this while loop?

(*dst++ = *src++)

Thanks.

  • Detail "why they declare src a const char and not just a char" --> `src` is not a `const char`. `src` is a `const char *`. `src` is a pointer. – chux - Reinstate Monica May 25 '20 at 01:43
  • (1) `(*dst++ = *src++)` copies one character from source to destination and advances both pointers, so the next time it will copy the next character. Since the result of the assignment is the value assigned, the loop will stop after it copies a NUL, which must be at the end of every string. (2a) So that if it is also a function-like macro, it won't get expanded. (2b) The return type is `char *` because it returns a pointer to a character array, aka a string. (3) To inform the world that it doesn't modify `src`. // Try to ask one question per post, and try to better explain your confusion. – rici May 25 '20 at 01:49
  • but so is char *dst. So why use a const? –  May 25 '20 at 01:49
  • Because it does modify `dst`. – rici May 25 '20 at 01:50
  • so rici, if I wanted to copy from one array to another, could I just say `while ((a++ = b++) != '\0') without the pointer references, outside of a function like this with an empty body, is that acceptable syntax? And perhaps more efficient than a for loop doing the same? –  May 25 '20 at 01:52
  • @AndrewB: No. Please reread the chapter in your C book about pointers. – rici May 25 '20 at 01:53
  • Ok. I dont have a c book. I am learning from online classes and courses. This is just something no one has covered. –  May 25 '20 at 01:55
  • This one isn't awful: https://en.wikibooks.org/wiki/C_Programming/Pointers_and_arrays#Pointers_and_Text_Strings – rici May 25 '20 at 02:14

1 Answers1

0

(*dst++ = *src++)

There is a lot going on here. The first thing to note is that this is assigning the value of *src++ to *dst++. The asterisk dereferences both dst++ and src++, so this is an assignment of one char to another. Note that the ++ operator has higher precedence than the * (dereference) operator. See https://en.cppreference.com/w/c/language/operator_precedence.

The second important detail is that the increments are postfix; ++ is to the right of the variables dst and src. So dst and src are only incremented after the check inside the while is complete. Effectively, this sets the first character in dst to be the first character in src.

So, dst and src increment by one each time the first character of src is not the zero character \0.

For your second question, see What do the parentheses around a function name mean?.

src is declared a const char * because the character(s) it points to are not being modified and this is generally good practice. There are lots of discussions on this on the internet; e.g. https://dev.to/fenbf/please-declare-your-variables-as-const. Note that src itself (the pointer) is clearly modified in the while loop.

Alex
  • 947
  • 6
  • 16