Need to understand how char * strcpy (char *cad1, const char *cad2) works in C
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;
}'
strcpy
add a comment |
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;
}'
strcpy
Hi @LauraCooper, welcome to StackOverflow. You may take a look to 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.
– 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 ofcad2
intocad1
and returns a pointer tocad1
. It works by iteratingcad2
char by char, and assigning them tocad1
. Untilcad2
ends (in a), 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 originalcad1
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.
– Groo
Nov 19 '18 at 23:52
add a comment |
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;
}'
strcpy
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;
}'
strcpy
strcpy
edited Nov 19 '18 at 23:39
Amy
21.6k1874131
21.6k1874131
asked Nov 19 '18 at 19:43
Laura CooperLaura Cooper
31
31
Hi @LauraCooper, welcome to StackOverflow. You may take a look to 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.
– 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 ofcad2
intocad1
and returns a pointer tocad1
. It works by iteratingcad2
char by char, and assigning them tocad1
. Untilcad2
ends (in a), 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 originalcad1
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.
– Groo
Nov 19 '18 at 23:52
add a comment |
Hi @LauraCooper, welcome to StackOverflow. You may take a look to 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.
– 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 ofcad2
intocad1
and returns a pointer tocad1
. It works by iteratingcad2
char by char, and assigning them tocad1
. Untilcad2
ends (in a), 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 originalcad1
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.
– Groo
Nov 19 '18 at 23:52
Hi @LauraCooper, welcome to StackOverflow. You may take a look to 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.
– Luis Miguel Mejía Suárez
Nov 19 '18 at 21:35
Hi @LauraCooper, welcome to StackOverflow. You may take a look to 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.
– 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
), 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
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
), 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
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.– Groo
Nov 19 '18 at 23:52
@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.– Groo
Nov 19 '18 at 23:52
add a comment |
2 Answers
2
active
oldest
votes
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.
1
Thank you so much! I understood everything perfectly, great explanation
– Laura Cooper
Nov 22 '18 at 19:00
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381595%2fneed-to-understand-how-char-strcpy-char-cad1-const-char-cad2-works-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
1
Thank you so much! I understood everything perfectly, great explanation
– Laura Cooper
Nov 22 '18 at 19:00
add a comment |
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.
1
Thank you so much! I understood everything perfectly, great explanation
– Laura Cooper
Nov 22 '18 at 19:00
add a comment |
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.
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.
answered Nov 19 '18 at 23:56
rod.poli.dinizrod.poli.diniz
116118
116118
1
Thank you so much! I understood everything perfectly, great explanation
– Laura Cooper
Nov 22 '18 at 19:00
add a comment |
1
Thank you so much! I understood everything perfectly, great explanation
– Laura Cooper
Nov 22 '18 at 19:00
1
1
Thank you so much! I understood everything perfectly, great explanation
– Laura Cooper
Nov 22 '18 at 19:00
Thank you so much! I understood everything perfectly, great explanation
– Laura Cooper
Nov 22 '18 at 19:00
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 20 '18 at 0:04
johnathanjohnathan
2,158819
2,158819
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381595%2fneed-to-understand-how-char-strcpy-char-cad1-const-char-cad2-works-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hi @LauraCooper, welcome to StackOverflow. You may take a look to 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.
– 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
intocad1
and returns a pointer tocad1
. It works by iteratingcad2
char by char, and assigning them tocad1
. Untilcad2
ends (in a), 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 originalcad1
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.– Groo
Nov 19 '18 at 23:52