I am presently new to programming and am following the C language which is being taught to us in our College. I have doubt in the actual functioning of the strcpy()
function under the header-file #include<strings.h>
The general use of this function is given below -
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char a[] = "google";
char *b = (char*)malloc((strlen(a) +1)*sizeof(char));
strcpy(b,a);
printf("%s, %s\n", a, b); //prints google, google
free(b);
}
We are given the following format and prototype of the same:
char *strcpy( char *to, const char *from)
{
char *temp = to;
while (*to++ = *from++);
return temp;
}
I have a couple of doubts about this-
What is an extra pointer ( temp ) requirement in the function? Can't we just return to;
at the end of the function?
I convinced myself that the temp variable is used because during each while loop, the pointer to
is changing itself. That is first *to
will be assigned *from
and then to = to +1
, and from = from +1
, will take place which will result in changing the address stored in the pointer to
. But if this is true, then what about the pointer from
? Will, it also not change at the end of the strcpy()
function?
Where is the function returning the address stored in temp? ( the function should only work if b = strcpy(b,a);
be mentioned not only strcpy(b,a);
)
To check the given function I also made my own function "strcpy_me" and used it with the same format and prototype, and it gave the same results.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *strcpy_me( char *to, const char *from)
{
char *temp = to;
while (*to++ = *from++);
return temp;
}
int main()
{
char a[] = "google";
char *b = (char*)malloc((strlen(a) +1)*sizeof(char));
strcpy_me(b,a);
printf("%s, %s\n", a, b); // prints google, google
free(b);
}
Please help me clarify my doubt.
see 2
It's a requirement from the implementation of strcpy to return a pointer to the beginning of the destination string. So if you want to implement your function in the same way as strcpy
, you'll have to save a copy of the original pointer passed as parameter, in case you intend to change that parameter inside the function. This isn't a particularly useful or rational feature, but strcpy has been like that since the dawn of time.
Not sure what you mean; here obviously: return temp;
return to;
is wrong. You have to realize that the function returns a value whether that value is used by the caller or not. Strict compilers might warn "value from function isn't used" in which case you'd have to write (void) strcpy(a,b);
to silence it. Since your example doesn't use the returned value, it doesn't matter that it is wrong.
What you think in no. 2 is correct.
from
does get changed but we don't need the previous value of from
. to
was stored in temp
as to
's original value should be returned, so we need it but no need for from
.
No, you don't need to do that. You can ignore return values in C(in most cases without warnings as well). No returning works as doing *to = *from
changes the parameter's memory as well, in this case, b. So, *to = *from
changes the array b
directly. It is because to
and b
store the same memory.
Note: In your while loop, it's *to++ = *from++
. It uses post addition, which for simplification, I didn't include here but all it does is increment both to
and from
after the operation.
As I said in 3, you don't need the return value. In fact, because original to
and temp
point to the same memory as b
, the function returns b
itself. doing, b = strcpy_me(b, a)
is the same as:
strcpy(b, a); b = b; //b's value stays the same
And yes, return temp;
is absolutely garbage in my opinion. It only causes confusion.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.