0

Can't get how a method with this head: char * strcpy (char *cad1, const char *cad2), works in C in this sample:

'char * strcpy (char *cad1, const char *cad2){
        char *aux = cad1;
        for( ; *cad1++ = *cad2++; );
        return cad1;
 }'
  • Hi @LauraCooper, welcome to StackOverflow. You may take a look to [how to ask](https://stackoverflow.com/help/how-to-ask) to improve this and future questions. Specially, you should provide some research effort to probe you already tried to solve your problem by yourself. Specially this isn't a site for asking for _"explain this code please"_. [See here for more information](https://meta.stackoverflow.com/questions/253894/how-to-handle-explain-how-this-code-dump-works-questions). – Luis Miguel Mejía Suárez Nov 19 '18 at 21:35
  • Anyways, my C is rusty, but basically what the function does is to copy the contents of `cad2` into `cad1` and returns a pointer to `cad1`. It works by iterating `cad2` **char** by **char**, and assigning them to `cad1`. Until `cad2` ends _(in a `\0`)_, that breaks the loop. This explanation is very vague and someone more familiar with C could gave it better. _(Also note this isn't a safe implementation of copy, since `cad2` can be much larger than the original `cad1` and then overriding, possibly already assigned, memory outside it.)_ – Luis Miguel Mejía Suárez Nov 19 '18 at 21:44
  • I think the return value is wrong here. – Greg Schmidt Nov 19 '18 at 23:08
  • @johnathan: the value of the `(*cad1++ = *cad2++)` expression is actually `*cad2` before the increment. I believe your suggestion wouldn't write the null terminated char. – vgru Nov 19 '18 at 23:52

2 Answers2

0

Starting from the method signature or prototype, that tells a lot about the how it works: we have two parameters together with their respective types and a return type. All parameters in this case are pointers to char, more known as char pointers. Those char pointers are what is used in "C" as strings of characters. One parameter is a const, because that value must not be changed in the function, it MUST keep, the original value. Strings in "C" have some peculiarities, once the pointer is created to a string it always points to the first characters in the string or index 0, the same as char *v = var[0], and can be incremented passing to the next char in the string such as v++. Other peculiarity in "C" is that all strings represented by char arrays end with a 0 character (ASCII null = 0).

The strcpy version account on that concepts and makes a for loop to copy each element in the char *cad2 to *cad1, that variables MUST be allocated statically or dynamically (malloc) before calling the function, and the return of the function in the code above is a pointer to the original variable (in that case *cad1, normally they return the copied one). In your function it was changed, I mean it is returning the original instead of the copied what looks wrong since you catch in the aux the pointer to the first element of the copied variable and you did not use it.

One good point to observe is the for loop:

for( ; *cad1++ = *cad2++; );

How it works is tricky, the first interesting point is that the for loop has tree parameters, and in "C" all are optional. The first is to initialize, the second is a boolean condition to continuing iterating, and the last one is to increment or decrement. Next, tricky is is *cad1++ = *cad2++ a boolean expression? The answer is yes, it is. Since in "C" the value 0 (zero) is false, and anything else is true. Remember that I have said strings in "C" finishes always with a 0 (zero), so when evaluating and assigning to the copy the value of a pointer (using *cad1 will return the value pointed by a pointer variable, the star in the begin makes that magic) and reaches the end of the string that will return false and finish the iteration loop. One point is interesting here, first the evaluation has less priority than the assignment in this case, what makes first the value being copied to the copy variable, then evaluating the boolean expression.

"C" is like this you writes a small code that have large meaning behind it. I hope you have understood the explanation. For further information have a look in "C" pointers at : https://www.tutorialspoint.com/cprogramming/c_pointers.htm.

rod.dinis
  • 1,233
  • 2
  • 11
  • 20
0
char * strcpy (char *cad1, const char *cad2){

for( ; *cad1++ = *cad2++;); 
return cad1; 
}

the way this works, at the calling side, it can be used in two ways, but always requires a buffer to write to so the use is simmilar.

 char arr[255];
 memset(arr,0,sizeof(char) * 255); // clear the garbage initialized array;
 strcpy(arr, "this is the text to copy that is 254 characters long or shorter.");
 puts(arr);

or

 char arr[255];
 memset(arr,0,sizeof(char) * 255);
 puts(strcpy(arr,"hello C!"));

sense the function returns the pointer to the buffer this works as well.

johnathan
  • 2,315
  • 13
  • 20