-1

This is my code. I am trying to simulate strcpy(). This code works, but I have a couple questions.

#include <stdio.h>
#include <stdlib.h>

char *strcpy(char *d, const char *s);

int main()
{
    char strOne[] = "Welcome to the world of programming!";
    char strTwo[] = "Hello world!";
    printf("String One: %s\n", strOne);
    printf("String Two before strcpy(): %s\n", strTwo);
    strcpy(strTwo, strOne);
    printf("String Two after strcpy(): %s\n", strTwo);

    return 0;
}

char *strcpy(char *d, const char *s)
{
   while (*s)
   {
       *d = *s;
       d++;
       s++;
   }
   *d = 0;
   return 0;
}
  1. When *s gets incremented to the position where '\0' is stored in the array, is that when the while condition becomes false because of '\0'? Does while read '\0' or just '0'?

  2. 'while' condition will be true if it reads '1'. All previous values of *s should be read as single characters in while condition, but the loop executes anyways. Why does this happen? Do all the single characters from the array *s is pointing equate to the value '1'?

  3. What does *d = 0; exactly do? The way I understand it, the copying process is completed when the while loop is exited. So why does removing *d = 0 lead to an incorrect output being displayed?

Output without *d = 0 :

String Two before strcpy(): Hello world!                                                                                       
String Two after strcpy(): Welcome to the world of programming! programming! 

Output with *d = 0 :

String Two before strcpy(): Hello world!                                                                                       
String Two after strcpy(): Welcome to the world of programming! 
supahsonic
  • 19
  • 1
  • `'\0' == 0`; `'0' == 48` (ASCII) – Fiddling Bits Jan 28 '20 at 19:24
  • 2
    This looks like a homework dump. To find the answers to your questions, google 'nul terminated strings' and how to use true and false in C. – klutt Jan 28 '20 at 19:25
  • 1
    The use of `strcpy()` as shown invokes [undefined behaviour](https://stackoverflow.com/q/2397984/15168) (UB). You are appending to an array that has no space reserved for extra characters. You may find it works mostly sanely — that's a possible result of UB. Your program might crash, or do something completely dolally — those are also possible results of UB. – Jonathan Leffler Jan 28 '20 at 19:39

2 Answers2

1

The characters in the ASCII table assume values that range from 0 to 127, 0 being NULL or '\0', so the condition is always true unless the character is '\0'.

*d = 0 places a '\0' at the end of the string; it's how strings are terminated in C. If you don't terminate the string, anything can be printed past the end of the string, and the program has no way to know where it ends. It's undefined behaviour.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • It all makes sense now. I couldn't understand `*d = 0` because I was reading the code wrong. Didn't take into account the fact that *d had been incremented in the while loop and that `\0` wasn't added to the end of the string by the while loop. Lesson learnt. Thanks for the help! – supahsonic Jan 28 '20 at 19:44
  • @supahsonic Glad to help. – anastaciu Jan 28 '20 at 19:50
0

Some more remarks. You return 0 instead of pointer to char. You should get some warnings. Return the copy instead. BTW this function can be simplified a bit as well.

char *strcpy(char *d, const char *s)
{
   char *saved = d;
   while ((*d++ = *s++));
   return saved;
}
0___________
  • 60,014
  • 4
  • 34
  • 74