Learn C: String Copier

Hey ya’ll

I’m doing this particular exercise in the Learn C course, its really engaging and I enjoyed it but I can’t understand the reasoning in the task 2 hint of the String Copier objective .
The code I’ve written was in this void shell that was provided at the beginning of the exercise.

void copy(char* dst, char* src){
  while(*src != '\0')
  {
    strcpy(dst, src);
    src++;
    dst++;
  }
  *dst = '\0';
}

Where strcpy(dst, src); this seems to work great. However the Hint requires us to use the dereference instead strcpy(*dst, *src); I’ve tried both and the latter throws all sorts of issues.
Screenshot 2021-12-29 at 15.48.35

Here’s the code:

void copy(char* dst, char* src){
  while(*src != '\0')
  {
    strcpy(dst, src);
    src++;
    dst++;
  }
  *dst = '\0';
} 
 
int main(){
  char srcString[] = "We promptly judged antique ivory buckles for the next prize!";
  int len = strlen(srcString) + 1;
  char dstString[len];

  //strcpy(dstString, srcString);
  copy(dstString, srcString);
  printf("%s\n", srcString);
  printf("%s\n", dstString); 
}

I commented out strcpy() in the body as I was trying things out.

So am I correct in my code? or did I misunderstand the hint?
Thank you. (also I’d love to see some game building courses using C++ and C#) Love me a bit of Half-Life.

Took my a few rereads of the projects, but I don’t think they want you to use strcpy().

strcpy() can already copy an entire string, to use it in a loop like this is horribly ineffecient since it’s constantly rewriting it work with the same thing.

Lets see if I can write an example out:

Say we have a string, str1, and we start copying it with the above code

str1 = "abc#"  // our source string
str2 = "####"  // and an empty destination

// For the sake of clarity, # will represent a null character

On the first iteration the pointers are on the first character:

"abc#"
 ^
"####"
 ^

This means strcpy() copies the entire string from the source to the destination and the pointers, but then moves the pointers and does the same again:

"abc#"
  ^
"abc#"
  ^

Now we copy portions of the string over and over again.

Rather I think they want you to update each individual char in the string, which is why the mention dereferincing.

1 Like

Oh I see, many thanks for that!

I’ve redone it now, it seems to be working okay. However it also seems to accept both ways of incrementing

src++;
dst++;

and

*src++;
*dst++;

That can’t be right.

1 Like

To my knowledge ++ has precedence over the dereferencer *, so the second version *src++ will work, though it has unneeded code.

Essentially you’ll update the pointer and then take its content. Though since there’s no assignment that content is ignored. gcc may actually give you a warning if you do this.

1 Like

Hi again
You’re right! I just found an explanation on GeeksforGeeks that explains that.
To override this one would need to add parentheses! Makes sense now that the order of precedence is clear.

Thank you very much, I found a nice nugget on StackOverFlow as well that I’ll try to work on :slight_smile:

1 Like